JavaScript中的map方法
map
方法会迭代数组中的每一个元素,并根据回调函数来处理每一个元素,最后返回一个新数组。注意,这个方法不会改变原始数组。
array.map(function(currentValue,index,array), thisValue);
thisValue:可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined";
对于map,一般的用法为:
1 var oldArray = [1,2,3,4,5]; 2 var newArray = oldArray.map(function(val){ 3 return val + 3; 4 }); 5 console.log(newArray);//[4,5,6,7,8]