ES6 find 和 filter 的区别

遇到个功能是要分类就想说在前端过滤,不要从查数据库的时候过滤了。然后就想说除了filter还有啥好用的

发现有个find,测试一番之后发现

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')

let list4 =list.findIndex(i=>i.name==='1')

console.log(list); [ { name: '1', index: 1 }, { name: '2' }, { name: '1' } ]
console.log(list2); { name: '1', index: 1 }

console.log(list4);0
console.log(list3);[ { name: '1', index: 1 }, { name: '1' } ]
find 和 filter 都是不改变原数组的方法

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

,而filter返回全部结果仍然是数组。

findIndex只查出第一个符合条件的结果,直接返回元素位置。



原文链接:https://blog.csdn.net/qq_39652441/article/details/79248127

原文地址:https://www.cnblogs.com/xqschool/p/15513077.html