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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-12
相关资源
相似解决方案