gpt4 book ai didi

javascript - lodash/underscore 通过数组值中的键查找对象

转载 作者:行者123 更新时间:2023-11-30 16:49:48 24 4
gpt4 key购买 nike

我有这个对象数组:

[
{
id: 1,
name: 'test 1'
},
{
id: 2,
name: 'test 2'
},
{
id: 3,
name: 'test 3'
},
{
id: 4,
name: 'test 4'
}
]

我有这个 ID 数组:

[1, 3]

如何选择 IDs 数组中存在 id 属性的所有对象?

最佳答案

var ids = [1, 3];
var found = _.where(items, function (item) {
return ids.indexOf(item.id) !== -1;
});

关于javascript - lodash/underscore 通过数组值中的键查找对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30699215/

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