js 数组API之forEach、map的用法

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]

 

 

posted @ 2017-12-18 15:05  狂奔的小马扎  阅读(1608)  评论(0编辑  收藏  举报