forEach和map的区别

写法上没什么区别,只是返回值会不一样,map能够返回每一项,而forEach则返回undefined,以后要用哪个你知道了吧?
map返回新的数组,可以进行后续更多的操作,例如:

let arr = [1, 2, 3, 4, 5];
let arr2 = arr.map(num => num * 2).filter(num => num > 5);
// arr2 = [6, 8, 10]

猜你喜欢

转载自www.cnblogs.com/whq920729/p/10665102.html