如何按数组过滤数据

Posted

技术标签:

【中文标题】如何按数组过滤数据【英文标题】:How to filter data by array 【发布时间】:2022-01-23 01:07:44 【问题描述】:

我有数据数组,我想按数组过滤数据。

下面是我的数组。

[1,3,4]

以下是我的数据

[
    id: 1, content_tag: 'Test'
    id: 2, content_tag: 'test 1'
    id: 3, content_tag: 'test 2'
    id: 4, content_tag: 'test 3'
]

我想要如下结果:

[
    id: 1, content_tag: 'Test'
    id: 3, content_tag: 'test 2'
    id: 4, content_tag: 'test 3'
]

【问题讨论】:

你必须展示你到目前为止尝试了什么,你遇到了什么问题 【参考方案1】:

使用数组方法过滤和包含来比较两个数组

let arr1 = [1,3,4]

let arr2 = [
    id: 1, content_tag: 'Test',
    id: 2, content_tag: 'test 1',
    id: 3, content_tag: 'test 2',
    id: 4, content_tag: 'test 3'
]

arr2.filter(eachElement => arr1.includes(eachElement.id))

【讨论】:

以上是关于如何按数组过滤数据的主要内容,如果未能解决你的问题,请参考以下文章