Languages
[Edit]
EN

JavaScript - check if array contains object with property equal to given value

0 points
Created by:
Wayne
475

In this article, we would like to show you how to check if array contains object with property equal to given value using JavaScript.

Quick solution:

const array = [
    {name: 'item-1'}
];

if (array.some((item) => item?.name === 'item-1')) {
    // do something when item exists
}

or: 

const array = [
    {name: 'item-1'}
];

const items = array.filter((item) => item?.name === 'item-1'); // finds multiple items

if (items.length > 0) {
    // do something when items exists
}

Note: the solutions presented in this article were introduced in ES5.

 

Practical examples

It is good to use optional chaining to prevent errors when object or some property may be undefined or null​​​​​.

Note: The optional chaining (?.) has been introduced in ES2020.

1. Using Array filter() method

In this example, we use filter() method to create a new array of items from users array with name property equal to the given value. If the length of the new array is greater than 0, it means that the array contains such an object.

// ONLINE-RUNNER:browser;

const users = [
	{ name: 'Kate', age: 25 },
	{ name: 'Tom',  age: 23 },
	{ name: 'Ann',  age: 26 },
	{ name: 'Jack', age: 21 }
];

if (users.filter(element => element?.name === 'Tom').length > 0) {
    console.log('user found');
}

2. Using Array some() method

In this example, we use some() method with optional chaining (?.) to check if users array contains at least one object with name property equal to the given value.

// ONLINE-RUNNER:browser;

const users = [
	{ name: 'Kate', age: 25 },
	{ name: 'Tom',  age: 23 },
	{ name: 'Ann',  age: 26 },
	{ name: 'Jack', age: 21 }
];

if (users.some((item) => item?.name === 'Tom')) {
    console.log('user found');
}

Solution for older browsers

In this section, we present an alternative solution that works for older browsers.

// ONLINE-RUNNER:browser;

const users = [
	{ name: 'Kate', age: 25 },
	{ name: 'Tom',  age: 23 },
	{ name: 'Ann',  age: 26 },
	{ name: 'Jack', age: 21 }
];

if (users.filter(function(user) { return user.name === 'Tom'; }).length > 0) {
    console.log('user found');
}

 

See also

  1. How to use optional chaining in JavaScript ES2020?

  2. JavaScript - basic array operations (map, filter, find, fill, some, every methods)

References

  1. Array.prototype.filter() - JavaScript | MDN
  2. Array.prototype.some() - JavaScript | MDN
  3. Optional chaining (?.) - JavaScript | MDN

Alternative titles

  1. JavaScript - determine if array contains object with attribute that equals given value
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join