forEach()遍历数组,对数据的操作会改变原数组。
map()不会改变原数组的值,返回一个新数组,新数组中的值为原数组调用函数处理之后的值
test1() {
var arr1 = [0, 2, 4, 6, 8];
var newArr1 = arr1.forEach(function (item, index, arr1) {
arr1[index] = item / 2;
});
console.log(arr1); //从控制台看出原数组发生改变
console.log(newArr1); // undefined
},
test2() {
var arr2 = [0, 2, 4, 6, 8];
var newArr2 = arr2.map(function (item, index, arr2) {
return item / 2;
});
console.log(arr2); //从控制台看出原数组并没有发生改变
console.log(newArr2);
}