0

Have an array of objects

var objArray = [{name: orange, id: 1},{name : apple, id:2},{name: banana, id:3},{name: grapes, id:4}]

and an array of id's

var arrId = [1,4]

How can i filter to get corresponding object of matching id from array of objects?

Expected:

var result = [{name: orange, id: 1}, {name: grapes, id:4}]

Tried:

objArray.filter(o => o.id === arrId);
1

1 Answer 1

1

You could check with Array#includes.

var objArray = [{ name: 'orange', id: 1 }, { name: 'apple', id: 2 }, { name: 'banana', id: 3 }, { name: 'grapes', id: 4 }],
    arrId = [1, 4],
    result = objArray.filter(o => arrId.includes(o.id));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.