- forEach()方法
不改变原数组,没有返回值,对数组每一项运行传入的函数,本质上forEach()方法相当于使用for循环遍历数组。
eg:
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
number.forEach((item, index, array) => {
//执行某些操作
});
- map()方法
不改变原数组,返回一个新数组,对数组每一项运行传入的函数,新数组中的每一项都是对原始数组中同样位置运行传入函数而返回的结果。
eg:
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
let mapResult = numbers.map((item, index, array) => item * 2);
alert(mapResult);// [2, 4, 6, 8, 10, 8, 6, 4, 2]