简体   繁体   中英

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:

[1, 3]

How can I select all objects whose id property exists in the IDs array?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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