Search

How to Check If an Array Includes an Object in JavaScript

post-title

Use the JavaScript some() Method

You can use the JavaScript some() method to find out if a JavaScript array contains an object.

This method tests whether at least one element in the array passes the test implemented by the provided function. Here's an example that demonstrates how it works:

<script>
    // An array of objects
    var persons = [{name: "Harry"}, {name: "Alice"}, {name: "Peter"}];
    
    // Find if the array contains an object by comparing the property value
    if(persons.some(person => person.name === "Peter")){
        alert("Object found inside the array.");
    } else{
        alert("Object not found.");
    }
</script>