forEach() 没有返回值!!!!
arr[].forEach(function(value,index,array){
xxxxx
})
参数:value数组中的当前项,index当前项的索引,array原始数组;
数组中有几项,那么传递进去的匿名回调函数就需要执行几次
理论上这个方式是没有返回值的,只是遍历数组中的每一项,不对原来数组进行修改,但是可以自己通过数组的索引来修改原来的数组
举例:
var array = [10,34,57,43,76];
var res = array.forEach(function (item,index,input) {
input[index] = item*10;
})
console.log(res);//--> undefined;
console.log(array);//--> 通过数组索引改变了原数组;
[100,340,570,430,760]
map() 有返回值,可以return出来!!!!
arr[].map(function(value,index,array){
xxx
return xxx
});
参数:value数组中的当前项,index当前项的索引,array原始数组
区别:map的回调函数中支持return返回值,return的是啥,相当于把数组中的这一项变为啥(并不影响原来的数组,只是相当于把原数组克隆了一份,把 克隆这一份的数组中的对应项改变了 );
var array = [10,34,57,43,76];
var res = array.map(function (item,index,input) {
return item*10;
})
console.log(res);//100,340,570,430,760
console.log(array);不变
$.each()
敲黑板:没有返回值,里面的匿名函数支持两个参数:当前项的索引i,数组中的当前项v,如果遍历的是对象,k是键,v是值
$.each(arr,function(index,value){
xxxx
})
参数:arr要遍历的数组,index当前的索引,value数组中的当前项
第一个和第二个参数正好和以上连个函数是相反的,注意不要记错了
$.map()
敲黑板:
特殊情况:
$("span").map()形式 ,参数顺序和each的是一样的