Mirza Delic
Mirza Delic

Reputation: 4339

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?

Upvotes: 1

Views: 909

Answers (1)

Connor Peet
Connor Peet

Reputation: 6265

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

Upvotes: 2

Related Questions