find filter区别

过滤数据,find 和 filter 都是不改变原数组的方法

但是 find 只查出第一个符合条件的结果;  像例子里是直接返回了一个对象 { } 而不是数组!

而 filter 返回【全部】结果仍然是数组

const list = [
    {'name':'1',index:1},
    {'name':'2'},
    {'name':'1'}
]
let list2 = list.find(i=>i.name==='1') 

let list3 = list.filter(i=>i.name==='1')

//find 对象  查出第一个
console.log(list2);  // { name: '1', index: 1 }

//filter 数组 查出全部
console.log(list3); // [ { name: '1', index: 1 }, { name: '1' } ]

 

posted @ 2020-10-24 16:43  我自是年少  阅读(283)  评论(0编辑  收藏  举报