js 数组API之forEach、map的用法

时间:2022-01-17 10:38:19

forEach语法:

arr.forEach(function(value, index, array){……})

实例:

// forEach
var arr = [,,,,];
arr.forEach(function(value, index, array){
array[index] *= ;
});
console.log(arr); //[2,4,6,8,10]

map语法:

var newArr = arr.map(function(value, index, array){return 要放入新数组的值})

实例:

// map
var arr = [,,,,];
var newArr = arr.map(function(value, index, array){
return value * ;
});
console.log(arr); // [1,2,3,4,5]
console.log(newArr); // [2,4,6,8,10]