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

标签 javascript underscore.js lodash

我有这个对象数组:

[
    {
        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/

相关文章:

javascript - 当 session 结束时执行 sails.js 中的代码

javascript - 数据表搜索不适用于列表类型的列

javascript - 是否可以为一个测试设置环境变量?

angularjs - 下划线,检查对象数组中是否存在键

javascript - 移动多维数组树的深层成员 - 使用 lodash 或下划线

javascript - 使用 lodash 按属性名称提取深度嵌套的子对象

javascript - 使用 TypeScript/JS/RxJS/map/reduce/filter 等将嵌套 JSON 转换为平面 JSON,

javascript - 方法未按正确顺序调用/异步方法

javascript - 下划线_.every方法是否同步?

javascript - 使用 lodash 查找匹配值(如果存在)