forEach语法:
arr.forEach(function(value, index, array){……})
实例:
// forEach var arr = [1,2,3,4,5]; arr.forEach(function(value, index, array){ array[index] *= 2; }); console.log(arr); //[2,4,6,8,10]
map语法:
var newArr = arr.map(function(value, index, array){return 要放入新数组的值})
实例:
// map var arr = [1,2,3,4,5]; var newArr = arr.map(function(value, index, array){ return value * 2; }); console.log(arr); // [1,2,3,4,5] console.log(newArr); // [2,4,6,8,10]
时间: 2024-09-28 00:55:55