Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove object from array using matching array

hey stackoverflow lets say I have an array such as this and an object displayed as below.

['first', 'name']
[{id: 'first'},{id: 'name'},{id: 'last'}]

I want my new object to be filtered out and return

[{id: 'first'}, {id: 'name'}]

How would I go about doing this in javascript?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use Array.filter and set the condition to whether the array contains the item’s id property:

const a = ['first', 'name']
const arr = [{id: 'first'},{id: 'name'},{id: 'last'}]

const res = arr.filter(e => a.includes(e.id))

console.log(res)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading