JavaScript array allows you to store multiple elements in a single object.
You can check if an array contains an object with a specific property value in JavaScript using the if (arrayName.some(e => e.propertyname === ‘value’)) statement.
Basic Example
let birdsObj = [
{ id: '100', name: 'owl'},
{ id: '101', name: 'dove'},
{ id: '102', name: 'parrot'}
];
if (birdsObj.some(e => e.name === 'owl')) {
console.log("Owl Object exists"); //Owl Object exists
} else {
//do nothing
}
This tutorial teaches you the different methods to check if an array contains an object with a specific property value in JavaScript and when it is appropriate to use each method.
Using Array Some
The array some() method checks if at least one element passes the test function specified.
It returns a boolean True
or False
based on the element’s existence.
Use this method when you want to check if an array contains an object with a property value and get a boolean value without the index information of the objects.
To check if an array contains an object with a specific property value,
- Invoke the
some()
method - Pass the test condition using the arrow function
- If an element exists with a specific property value, it returns
True
- If no element exists with a specific property value, it returns
False
.
Code
let birdsObj = [
{ id: '100', name: 'owl'},
{ id: '101', name: 'dove'},
{ id: '102', name: 'parrot'}
];
if (birdsObj.some(e => e.name === 'owl')) {
console.log("Owl Object exists"); //Owl Object exists
} else {
//do nothing
}
Using Array Filter
The array filter() method creates a shallow copy of the array with the objects that pass a specific test condition.
It returns an array with the elements that pass the test condition.
Use this method when you want to check if the array contains an object with a specific value for a property and need access to those objects instead of boolean values.
To check if an array contains an element with a specific property value,
- Invoke the
filter()
method - Pass the test condition using the arrow function
- It returns an array of objects with the property value
Now, check if the array is empty using the length
property.
- If the length is greater than 0, the element exists with the specific property value
- If the length is 0, the element doesn’t exist
Code
let birdsObj = [
{ id: '100', name: 'owl'},
{ id: '101', name: 'dove'},
{ id: '102', name: 'parrot' }
];
var owlArray = birdsObj.filter(e => e.name === 'owl');
if (owlArray.length > 0) {
console.log("Owl Object exists"); // Owl Object exists
} else {
//do nothing
}
Using FindIndex
The array findIndex()
method returns the index of the first element that passes the test condition.
If no elements match the condition, it returns -1
.
Use this method when you want to find the index of the first matching element.
To check if the array contains an element with a specific property value,
- Invoke the
findIndex()
method - Pass the test condition using the arrow function
- Check if the returned value is greater than
-1
- If yes, an element exists in the matching condition at the returned index position
- If the index is
-1
, the element doesn’t exist in the array
Code
let birdsObj = [
{ id: '100', name: 'owl'},
{ id: '101', name: 'dove'},
{ id: '102', name: 'parrot' }
];
const owlIndex = birdsObj.findIndex(e => e.name === 'owl');
if (owlIndex > -1) {
console.log("Owl Object exists"); //Owl Object exists
} else {
//do nothing
}
Using Reduce
If your array might contain more than one element with the specified value, use the reduce() method to find the index of all the elements that have a specific property value.
- It returns an array with the indexes of all matching elements.
- If no elements match the passed condition, an empty array is returned.
Code
let birdsObj = [
{ id: '100', name: 'owl'},
{ id: '101', name: ‘owl’},
{ id: '102', name: 'parrot' }
];
var owlIndexes = birdsObj.reduce((acc, {
name
}, index) => name === "owl" ? [...acc, index] : acc, []);
console.log(owlIndexes); // [0, 1]