js map()与forEach()遍历的区别
2017-03-13 15:30
519 查看
一、js map()和forEach()遍历
共同点:
1、都是遍历数组中的每一项
2、每一次执行匿名函数都支持3个参数,(item,index,input)item:当前项,index:当前项的索引,input:原始数组
3、匿名函数中的this都指向window
4、只能遍历数组
forEach():
没有返回值。仅仅是遍历数组中的每一项,不对原来数组进行修改;但是可以自己通过数组的索引来修改原来的数组;
eg:
var ary = [12,23,24,42,1];
var res = ary.forEach(function (item,index,input) {
input[index] = item*10;
})
console.log(res);//--> undefined;
console.log(ary);//--> 通过数组索引改变了原数组;
map():
有返回值.
var ary = [12,23,24,42,1];
var res = ary.map(function (item,index,input) {
return item*10;
})
console.log(res);//-->[120,230,240,420,10]; 原数组拷贝了一份,并进行了修改
console.log(ary);//-->[12,23,24,42,1]; 原数组并未发生变化
着重说一下map():
["1","2","3"].map(parseInt); // 输出1,NAN,NAN
首先map()接收两个参数,一个回调函数callback,一个回调函数的this值。而题目中map只传入了回调函数--parseInt
回调函数支持3个参数item,index,input。而parseInt只接收两个参数string、radix(基数)
radix基数是可选的。该值介于2-36之间。如果省略该参数或值为0,则数字将以10为基数来解析。如果以“0x”或“0X”开头将以16为基数。
如果该参数小于2或大于36,则parseInt()将返回NAN.
二、jquery的$.each()和$.map()遍历的区别
既可遍历数组又可遍历对象
$.each()是没有返回值的。里面的匿名函数只支持两个参数index,item
$.each(arr,function(index,item){
// do something
})
$('span').each(function(index,item){
alert(index + ":" + item);
})
注意:index,item的顺序(跟forEach顺序相反)
遍历对象:
$.each( { name: "John", lang: "JS" }, function(k, v){
alert( "Name: " + k + ", Value: " + v );
});
$.map()有返回值
$.map()里面的匿名函数支持2个参数和$.each()里的参数位置相反。
如果是$("span").map()形式,参数顺序和$.each() $("span").each()一样。
$.map(arr, function(value, index){
//do something
return XXX
})
map():
有返回值
共同点:
1、都是遍历数组中的每一项
2、每一次执行匿名函数都支持3个参数,(item,index,input)item:当前项,index:当前项的索引,input:原始数组
3、匿名函数中的this都指向window
4、只能遍历数组
forEach():
没有返回值。仅仅是遍历数组中的每一项,不对原来数组进行修改;但是可以自己通过数组的索引来修改原来的数组;
eg:
var ary = [12,23,24,42,1];
var res = ary.forEach(function (item,index,input) {
input[index] = item*10;
})
console.log(res);//--> undefined;
console.log(ary);//--> 通过数组索引改变了原数组;
map():
有返回值.
var ary = [12,23,24,42,1];
var res = ary.map(function (item,index,input) {
return item*10;
})
console.log(res);//-->[120,230,240,420,10]; 原数组拷贝了一份,并进行了修改
console.log(ary);//-->[12,23,24,42,1]; 原数组并未发生变化
着重说一下map():
["1","2","3"].map(parseInt); // 输出1,NAN,NAN
首先map()接收两个参数,一个回调函数callback,一个回调函数的this值。而题目中map只传入了回调函数--parseInt
回调函数支持3个参数item,index,input。而parseInt只接收两个参数string、radix(基数)
radix基数是可选的。该值介于2-36之间。如果省略该参数或值为0,则数字将以10为基数来解析。如果以“0x”或“0X”开头将以16为基数。
如果该参数小于2或大于36,则parseInt()将返回NAN.
二、jquery的$.each()和$.map()遍历的区别
既可遍历数组又可遍历对象
$.each()是没有返回值的。里面的匿名函数只支持两个参数index,item
$.each(arr,function(index,item){
// do something
})
$('span').each(function(index,item){
alert(index + ":" + item);
})
注意:index,item的顺序(跟forEach顺序相反)
遍历对象:
$.each( { name: "John", lang: "JS" }, function(k, v){
alert( "Name: " + k + ", Value: " + v );
});
$.map()有返回值
$.map()里面的匿名函数支持2个参数和$.each()里的参数位置相反。
如果是$("span").map()形式,参数顺序和$.each() $("span").each()一样。
$.map(arr, function(value, index){
//do something
return XXX
})
map():
有返回值
相关文章推荐
- 原生JS forEach()和map()遍历的区别以及兼容写法
- 原生JS forEach()和map()遍历的区别以及兼容写法,jQuery $.each()和$.map()遍历
- 原生JS forEach()和map()遍历的区别以及兼容写法
- 原生JS forEach()和map()遍历的区别以及兼容写法
- js---原生JS数组arr遍历方法forEach()和map()遍历的区别以及兼容写法
- js---js中数组遍历方法forEach与map()有什么区别?
- js数组遍历some,foreach,map,filter,every对比
- 原生js使用forEach()与jquery使用each遍历数组,return false 的区别
- JS中的forEach、$.each、map的区别
- 笔记-JS数组遍历方法之forEach()和map()
- js中foreach、each、map遍历数组
- JS中map、forEach、filter、reduce等Array新增方法的区别
- JS中some(),every(),forEach(),map(),filter()区别
- js中数组过滤、遍历、迭代every、some、filter、map、forEach、reduce、reduceRight
- js数组遍历some,foreach,map,filter,every对比
- js中foreach,for in,for of的使用及区别,遍历对象,遍历数组使用的不一样 JavaScript
- js数组中forEach/some/every/map/filter/reduce的区别
- JS的forEach和map方法的区别,还有一个$.each
- 原生js使用forEach()与jquery使用each遍历数组,return false 的区别
- JS中some(),every(),forEach(),map(),filter()区别