- 语法:forEach和map都支持2个参数:一个是回调函数callback(function(item,index,array){})和上下文context;
-
用法如下:::[].forEach(function(item,index,array){
//code something in here
});
- 用来遍历数组中的每一项;这个方法执行是没有返回值的,对原来数组也没有影响;
- 每一次执行匿名函数的时候,还给其传递了三个参数值:数组中的当前项item,当前项的索引index,原始数组input;
- 理论上这个方法是没有返回值的,仅仅是遍历数组中的每一项,不对原来数组进行修改;但是我们可以自己通过数组的索引来修改原来的数组;
- forEach方法中的this是ary,匿名回调函数中的this默认是window;
var ary = [1,2,3,4,5]; var res = ary.forEach(function (item,index,array) { array[index] = item*10; }) console.log(res); //undefined; console.log(ary); //结果为[10,20,30,40,50];
- map:即是 “映射”的意思 用法与 forEach 相似,都是用来遍历数组中的每一项值的,用来遍历数组中的每一项;
- 用法如下:::
[].map(function(item,index,array){
//code somthing in here
})
- 区别:map的回调函数中支持return返回值;return的是啥,相当于把数组中的这一项变为啥(并不影响原来的数组,只是相当于把原数组克隆一份,把克隆的这一份的数组中的对应项改变了);
- 不管是forEach还是map 都支持第二个参数值,第二个参数的意思是把匿名回调函数中的this进行修改。
var ary = [1,2,3,4,5];var res = ary.map(function (item,index,array) {
return item*10;
})
console.log(res); //结果为[10,20,30,40,50];
console.log(ary); //结果为[1,2,3,4,5];