【问题标题】:How to perform multiple array filtering如何执行多个数组过滤
【发布时间】:2023-02-08 23:00:20
【问题描述】:

我有两个具有共同 ID 的数组,因此我可以将它们连接在一起,但我需要过滤方面的帮助。

所以我有一个第一个数组的列表和它前面的复选框。 如果我选中一个复选框,我将执行过滤并显示表内第二个数组中的另一项(通过检查相互 ID)。

我需要帮助,因为如果我执行多项选择,我希望得到多个结果,而不仅仅是一个。这是图像的外观以及我用于过滤的代码。

  let items = [
    {id:1, name:'Item 1', appId:10},
    {id:2, name:'Item 2', appId:20},
    {id:3, name:'Item 3', appId:20},
    {id:4, name:'Item 4', appId:30}
  ]
  
  let apps = [
    {id:10, address:'Some street 1', city:'City 1'},
    {id:20, address:'Some street 2', city:'City 2'},
    {id:20, address:'Some street 2', city:'City 2'},
    {id:30, address:'Some street 3', city:'City 3'}
  ]

this.dataSource = this.items.filter(x => x.appId == apps.id)

谢谢

【问题讨论】:

  • 请修改您的帖子标题以提出明确、具体的问题。见How to Ask
  • 请创建一个minimal reproducible example。什么是app.iditems 和 " 是什么意思第二个数组“看起来像,它们是如何连接的?

标签: javascript angular


【解决方案1】:

如果两个数组相同,您可以使用:

let array1 = [...];
let array2 = [...];
let idx = array1.findIndex(x => x.id === app.id);
let result = [array1[idx], array2[idx]];

否则,您可以将两个数组连接在一起并一起过滤:

result = [array1, ...array2].filter(x => x.id === app.id);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 2017-08-30
    相关资源
    最近更新 更多