美文网首页
find filter区别

find filter区别

作者: 李亚_45be | 来源:发表于2019-07-11 18:57 被阅读0次

过滤数据,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')

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

console.log(list3); // [ { name: '1', index: 1 }, { name: '1' } ]

相关文章

网友评论

      本文标题:find filter区别

      本文链接:https://www.haomeiwen.com/subject/vvvdkctx.html