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

How to check whether an array of object is present in another array of object in Javascript

I have two array like this

const arr1 = [{code: 'a', name: 'x'}];
const arr2 = [{code: 'a', name: 'x'}, {code: 'b', name: 'y'}];

Here I want to check the code vale in first array object is present in the second array’s code value.

For this I tried like this but it returned false

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

arr1.every(item => arr2.includes(item))

How can I check the code value in first array object is present in the second array object

>Solution :

You can do something like this

const arr1 = [{code: 'a', name: 'x'}];
const arr2 = [{code: 'a', name: 'x'}, {code: 'b', name: 'y'}];

const codeValues = new Set(arr2.map(({code}) => code));

const isPresent = arr1.some(item => codeValues.has(item.code));
console.log(isPresent);
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