js中数组的some,every,map,filter方法的差别

js中数组的some,every,map,filter方法都是对数组中的每个元素进行操作,但是存在差别,话不多说放代码:

        let arr = [1, 2, 3, 4, 5]
        console.log(arr.some(function (key) {//true
            return key >= 3
        }));//some函数会所有数组里的元素执行callback,直到找到一个符合条件的元素,然后在返回true
        console.log(arr.every(function (key) {//false
            return key >= 3
        }));//every函数会所有数组里的元素执行callback,直到找到一个不符合条件的元素,然后在返回false
        console.log(arr.map(function (key) {//[false, false, true, true, true]
            return key >= 3
        }));//map函数会对所有的元素执行相同的操作,并且将执行的结果返回成一个新的数组
        console.log(arr.filter(function (key) {//[3, 4, 5]
            return key >= 3
        }));//filter函数会为所有元素执行相同操作,再将满足条件的数组提取出来        

  

posted @ 2020-11-22 19:36  coderLsq  阅读(69)  评论(0)    收藏  举报