js五种不同的遍历 (filter, map,foreach,every, some,)
Posted peko
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js五种不同的遍历 (filter, map,foreach,every, some,)相关的知识,希望对你有一定的参考价值。
var arr=[1,2,"a",2,4,1,4,"a",5,6,7,8,"aa","bb","c","a"]
// foreach
console.log(arr.forEach((item) => {
return item === "a" // undefined. foreach无返回值,只是遍历执行
}))
// map
console.log(arr.map((item) => {
return item === "a" //[false,false,true,...]返回一个新的Array,每个元素为调用func的结果
}))
// filter
console.log(arr.filter((item) => {
return item === "a" //[‘a‘,‘a‘,‘a‘] 返回一个符合func条件的元素数组
}))
// every
console.log(arr.every((item) => {
return item === "a" //false :返回一个boolean,判断每个元素是否符合func条件
}))
// some
console.log(arr.some((item) => {
return item === "a" //true ::返回一个boolean,判断是否有元素是否符合func条件
}))
以上是关于js五种不同的遍历 (filter, map,foreach,every, some,)的主要内容,如果未能解决你的问题,请参考以下文章
JS之JQ的map/reduce/filter/sort/reverse
js数组遍历some,foreach,map,filter,every对比
js数组遍历some,foreach,map,filter,every对比
js数组遍历some,foreach,map,filter,every对比