简体   繁体   English

lodash /下划线按数组值中的键查找对象

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

I have this array of objects: 我有这个对象数组:

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

I have this array of IDs: 我有这个ID数组:

[1, 3]

How can I select all objects whose id property exists in the IDs array? 如何选择ID数组中存在id属性的所有对象?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM