Lodash /underscore通过数组值中的键查找对象

lodash/underscore find objects by key that is in values of array

本文关键字:查找 对象 数组 underscore Lodash      更新时间:2023-09-26

我有这样一个对象数组:

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

我有这个id数组:

[1, 3]

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

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