首页 > 其他 > 详细

find filter区别

时间:2020-10-24 22:55:50      阅读:36      评论:0      收藏: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‘)

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

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

 

find filter区别

原文:https://www.cnblogs.com/yangsg/p/13869950.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!