23.说一说map 和 forEach 的区别?
得分点:
map创建新数组、map返回处理后的值、forEach()不修改原数组、forEach()方法返回undefined
标准回答:
map和forEach的区别:
①
map有返回值,可以开辟新空间,return出来一个length和原数组一致的数组,即便数组元素是undefined或者是null。
②forEach默认无返回值,返回结果为undefined,可以通过在函数体内部使用索引修改数组元素。
① map:
let aaa = this.bbb,map((item, index) => {
return item['ccc']
})
② forEach
this.bbb.forEach((item, index, array) => {
return this.aaa.push(item['ccc'])
})
加分回答
map的处理速度比forEach快,而且返回一个新的数组,方便链式调用其他数组新方法,比如filter、reduce
let arr = [1, 2, 3, 4, 5];
let arr2 = arr.map(value => value * value).filter(value => value > 10); // arr2 = [16, 25]
💞💖💓💗每个时代,
✨🌟⭐️💫都悄悄犒赏会学习的人。

563





