Categories
JavaScript Answers

How to determine if JavaScript array contains an object with an attribute that equals a given value?

Spread the love

To determine if JavaScript array contains an object with an attribute that equals a given value, we use the find method,.

For instance, we write

if (vendors.find((e) => e.name === "foo")) {
  //...
}

to call find with a function that checks if the name property of the objects in the vendors array has value 'foo'.

If it’s true, then the first object that matches the condition will be returned.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *