数组的5个迭代方法

数组的5个迭代方法
every()对数组中的每一项运行给定的函数,如果该函数对每一项都返回true,则返回true。
some()对数组中的每一项运行给定的函数,如果该函数对任一项返回true,则返回true。
filter()对数组中的每一项运行给定的函数,返回该函数会返回true的项组成的数组。
map()对数组中的每一项运行给定的函数,返回每次函数调用的结果组成的数组。
forEach()对数组中的每一项运行给定的函数,这个方法没有返回值。

标注:参数是个函数。

var arr=[2,3,2,4,22,3,1,34,32,13,0];
console.log(arr.every(function(item,index,array){
return item>3;
}))//false
console.log(arr.some(function(item,index,array){
// console.log(array);
return item>3;
}))//true
console.log(arr.filter(function(item,index,array){
// console.log(array);
return item>22;
}))//[34, 32]
console.log(arr.map(function(item,index,array){
return item+=1;
}))//[3, 4, 3, 5, 23, 4, 2, 35, 33, 14, 1]
arr.forEach(function(item,index,array){
// console.log(array);
if(item==3){
console.log(index);
}
})

猜你喜欢

转载自www.cnblogs.com/studyh5/p/9700399.html
今日推荐