Check if an Array contains an Object using Array.some() Method.
To check if an array contains an object:
- Pass the
some()
method on the array. - For each object in the array, check if it has a property with the specified value.
- The
some()
method will return true if the object is in the array.
Example code –
const people = [
{id: 1, name: 'Matthew'},
{id: 2, name: 'Weston'},
];
const isFound = people.some(element => {
if (element.id === 1) {
return true;
}
return false;
});
console.log(isFound);
if (isFound) {
// This code block run if array contain object
}
The some()
method is a way to test if at least one element in an array passes a test. The test is set up by a provided function. If the function finds at least one element that passes the test, it returns true. If it does not find any elements that pass the test, it returns false. The some()
method does not change the array.
The function we passed to the some()
method will get called with each value (object) in the array. It will return true if the function returns true for at least one of the values. Otherwise, it will return false.
In the example, The callback function is called with an object. If the object has an id property with a value of 1
, the function returns true
.
Check if an Array contains an Object using Array.find() Method.
To check if a JavaScript array contains an object:
- Pass the
find()
method on the array. - For each object in the array, check if it has a property with the specified value.
find()
will return the object if it is contained in the array.
Example –
const people = [
{id: 1, name: 'Matthew'},
{id: 2, name: 'Weston'},
];
const person = people.find(element => {
if (element.id === 1) {
return true;
}
return false;
});
console.log(person);
if (person !== undefined) {
// This code block will run if object found in array
}
The find()
method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, the find()
method will return undefined.
In the example – The object has an id property set to 1
, so the find()
method returns the object.
Note – find()
will return the value, some()
will return a boolean.
Check if an Array contains an Object using Array.findIndex() Method.
To check if a JavaScript array contains an object:
- Pass the
findIndex()
method on the array. - For each object in the array, check if it has a property with the specified value.
- The
findIndex()
method will return the index of the object in the array. If the object is not in the array it will return-1
.
Example –
const people = [
{id: 1, name: 'Matthew'},
{id: 2, name: 'Weston'},
];
const index = people.findIndex(element => {
if (element.id === 1) {
return true;
}
return false;
});
console.log(index);
if (index !== -1) {
// This code block will run if object found in array
}
The findIndex()
method works similarly to the find()
method, but instead of returning the element that satisfies the condition, it returns the index of that element.
ℹ️ The findIndex()
method returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.
The findIndex()
method calls its callback function with each element in the array until a truthy
value is returned. If the values in the array are exhausted, the callback function will not be called.
Check if an Array contains an Object using Array.filter() Method.
To check if a JavaScript array contains an object:
- Pass the
filter()
method on the array. - For each object in the array, check if it has a property with the specified value.
- The
filter()
method will return an array of objects that satisfy the condition.
Example –
const people = [
{id: 1, name: 'Matthew'},
{id: 2, name: 'Weston'},
];
const filteredArray = people.filter(element => {
if (element.id === 1) {
return true;
}
return false;
});
console.log(filteredArray);
if (filteredArray.length > 0) {
// This code block will run if object found in array
}
The filter()
method is invoked with every element in the array, which means that it checks every element instead of stopping at the first truthy
value like the other methods.
In the example, We check if each element in the array has an id
property with a value of 1
. If the new array is not empty, there are matching objects.
The filter()
method returns an array of all elements that satisfy the given condition. So, if we had multiple objects with an id
equal to 1
, all of them would be included in the new array.
Summary –
There are four ways to check if a JavaScript array contains an object:
- with Array.some() method.
- with Array.find() method.
- with Array.findIndex() method.
- with Array.filter() method.