【问题标题】:filter based in criteria of an array of objects inside an array in Javascript根据Javascript中数组内对象数组的条件进行过滤
【发布时间】:2022-06-13 23:20:31
【问题描述】:

我有这个数组:

defaultColumnsWithItems = [
{
  column: 'Contrie', items: [
  { id: 1, label: 'USA', selectedItem: true },
  { id: 2, label: 'FRANCE', selectedItem: false  },
  { id: 2, label: 'MAROC', selectedItem: false  }
  ]
},
{
  column: 'Categorie', items:
    [
      { id: 0, label: 'Alimentaion', selectedItem: true },
      { id: 1, label: 'ricolage', selectedItem: false },
      { id: 2, label: 'Literie', selectedItem: true },
      { id: 3, label: 'Menage', selectedItem: false },
    ]
}

];

我只想过滤具有等于 true 的选定项的元素。 我试试这个:

const columnsWithSelectedItems = colmunsWithItemsToFilter.filter((columnWithItems) => {
    return columnWithItems.items.filter( item => item.selectedItem === true)
  })

但它返回所有元素。

谢谢。

【问题讨论】:

  • 你忘了问问题。如果您在执行刚刚编写的内容时遇到问题,请提供有关您遇到的问题的信息,提供您的代码尝试、失败的输入、失败的原因以及预期的结果。展示你的努力。

标签: javascript angular typescript lodash


猜你喜欢
  • 2019-09-15
  • 1970-01-01
  • 2020-12-03
  • 2019-04-05
  • 2021-11-28
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多