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 if a certain value exist in an array which holds objects as its elements. JavaScript

The first one is when we have an array holds regular elements like strings and I know how to use Includes on it.

But how about arrays holding objects as their element?
How can I check if there is a certain value in those objects?

const arr = ['this', 'is', 'a test'];

console.log(arr.includes('this'));

const arr2 = [
  { id: '123', name: 'Alex' },
  { id: '345', name: 'Dan' },
  { id: '33', name: 'Joe' },
];

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 :

You can use the some function.

const arr2 = [
  { id: '123', name: 'Alex' },
  { id: '345', name: 'Dan' },
  { id: '33', name: 'Joe' },
];

console.log(arr2.some(item => item.name==="this" ))

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

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