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

Determine if object property includes array value

Given the following array

[60, 1456]

How can I determine which items in this object do not have a corresponding id to the numbers in the array?

[
  {id: 60, itemName: 'Main Location - Cleveland'},
  {id: 1453, itemName: 'Second Location - Cleveland'},
  {id: 1456, itemName: 'Third Location - New York'}
]

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 filter() combined with includes() to check if the id does not (!) exists in the ids array:

const ids = [60, 1456];
const data = [
  {id: 60, itemName: 'Main Location - Cleveland'},
  {id: 1453, itemName: 'Second Location - Cleveland'},
  {id: 1456, itemName: 'Third Location - New York'}
];

const res = data.filter(({ id }) => !ids.includes(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