数组中多个值的过滤方法Javascript [已关闭]

ffscu2ro  于 2023-05-12  发布在  Java
关注(0)|答案(3)|浏览(114)

已关闭,此问题需要details or clarity。目前不接受答复。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

20小时前关闭。
Improve this question
我有这个数组

var selectedId = [
  "12",
  "22"
];

var allData = [{
    "id": "12",
    "title": "Title 1",
    "description": "description"
  },
  {
    "id": "22",
    "title": "Title 2",
    "description": "description"
  },
  {
    "id": "25",
    "title": "Title 3",
    "description": "description"
  }
]

我想使用过滤器功能检查多个ID

this.allData.filter((people => people.id=='12'));

对于单个值,它是工作的,但我有1222,我想只得到这两个记录。
任何解决方案。谢谢

mepcadol

mepcadol1#

可以使用array.includes()

const selectedId = [
  "12",
  "22"
];

const allData = [
  {
    "id":"12",
    "title":"Title 1",
    "description":"description"
  },
  {
    "id":"22",
    "title":"Title 2",
    "description":"description"
  },
  {
    "id":"25",
    "title":"Title 3",
    "description":"description"
  }
]

const filtered = allData.filter((people => selectedId.includes(people.id))); 
console.log(filtered);
a11xaf1n

a11xaf1n2#

var selectedId = [
  "12",
  "22"
];

var allData = [{
    "id": "12",
    "title": "Title 1",
    "description": "description"
  },
  {
    "id": "22",
    "title": "Title 2",
    "description": "description"
  },
  {
    "id": "25",
    "title": "Title 3",
    "description": "description"
  }
];

console.log(allData.filter(people => selectedId.includes(people.id)));
hjqgdpho

hjqgdpho3#

您可以使用此方法:

this.allData.filter((people => selectedId.includes(people.id)))

相关问题