gpt4 book ai didi

javascript - 基于javascript中的另一个数组过滤对象数组

转载 作者:数据小太阳 更新时间:2023-10-29 04:57:55 25 4
gpt4 key购买 nike

给定一个对象数组:

people = [
{id: "1", name: "abc", gender: "m", age:"15" },
{id: "2", name: "a", gender: "m", age:"25" },
{id: "3", name: "efg", gender: "f", age:"5" },
{id: "4", name: "hjk", gender: "m", age:"35" },
{id: "5", name: "ikly", gender: "m", age:"41" },
{id: "6", name: "ert", gender: "f", age:" 30" },
{id: "7", name: "qwe", gender: "f", age:" 31" },
{id: "8", name: "bdd", gender: "m", age:" 78" },
]

还有一组想要的 ids:

id_filter = [1,4,5,8]

如何过滤数组 people 以返回 id_filtergender=m 中定义的目标 ID?

最佳答案

最简单的过滤方式是使用数组的filter()功能,类似于:

people.filter(person => id_filter.includes(person.id))

关于javascript - 基于javascript中的另一个数组过滤对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46894352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com