10

To get items from array that match array of values I use this:

var result =_(response).keyBy('id').at(arrayOfIDs).value();

How can I do the opposite? Get items that does not match array of values.

0

3 Answers 3

15

This is easily done with vanilla JS.

var nonMatchingItems = response.filter(function (item) {
    return arrayOfIDs.indexOf(item.id) === -1;
});

The same approach is possible with lodash's _.filter(), if you positively must use lodash.

ES6 version of the above:

var nonMatchingItems = response.filter(item => arrayOfIDs.indexOf(item.id) === -1);

// or, shorter
var nonMatchingItems = response.filter(item => !arrayOfIDs.includes(item.id));
Sign up to request clarification or add additional context in comments.

Comments

0

You don't need lodash, just use plain javascript; it's easier to read as well...

function getId (val) {
    return val.id;
}

function notMatchId (val) {
    return arrayOfIDs.indexOf(val) === -1;
}

var result = response.map(getId).filter(notMatchId);

1 Comment

Not really correct, this returns a list of IDs, the OP wants the associated items.
0

I think you're looking for the pullAll (https://lodash.com/docs/4.17.15#pullAll) function :

const result = _.pullAll([...response], arrayOfIDs);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.