给出以下数据:

const array = [
  {name: one, completed: false},
  {name: two, completed: false},
  {name: three, completed: false},
  {name: four, completed: false},
]

const arrayTwo = [
  {name: one, completed: true},
  {name: two, completed: true},
  {name: three, completed: false},
  {name: four, completed: false},
]


我需要能够返回:

//from array
[
  {name: one, completed: false}
]

//from arrayTwo
[
  {name: one, completed: true},
  {name: two, completed: true},
  {name: three, completed: false},
]


因此,问题来了:
我如何映射/过滤/减少这些数组以返回直到满足包括以下条件的第一个条件的所有项目:

completed: false


...或第一个(如果它们都满足以下条件):

completed: false


一如既往地感谢所有方向,在此先感谢!

最佳答案

  const result = array.slice(0, array.findIndex(it => !it.completed) + 1);

关于javascript - es6数组返回所有达到特定条件的项目,如果所有项目都满足条件,则返回第一个项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55637550/

10-11 14:02