定义
forEach()
: 针对每一个元素执行提供的函数(executes a provided function once for each array element)。
map()
: 创建一个新的数组,其中每一个元素由调用数组中的每一个元素执行提供的函数得来(creates a new array with the results of calling a provided function on every element in the calling array)。
到底有什么区别呢?
forEach()
方法不会返回执行结果,而是undefined
。也就是说,forEach()
会修改原来的数组。而map()
方法会得到一个新的数组并返回。