How to Check if an Array Contains a Value in Javascript

In this article, you’ll learn how to use and verify if an array has a value in javascript. In JavaScript, a value can be primitive like an integer or a text. It might also be an object. This article demonstrates how to determine whether an array has a value, whether it is a primary value or an object.

Check if an array contains a string

To check if an array contains a primitive value, you can use the array method like array.includes()

The following example uses the array.includes() method to check if the colors array contains ‘red’:

const colors = ['red', 'green', 'blue'];
const result = colors.includes('red');

console.log(result); // true

If you want to ignore the letter cases when checking, you can:

  • First, return a new array that contains all elements in lowercase using the map() and toLocaleLowerCase() methods.
  • Then, use the includes() method to check.

The following example illustrates the steps:

const colors = ['Red', 'GREEN', 'Blue'];
const result = colors.map(e => e.toLocaleLowerCase())
                     .includes('green');                          

console.log(result); // true

In this example, the colors array doesn’t contain ‘green’ but ‘GREEN’.

First, the map() method makes every element in the colors array lowercase and returns a new array. Then, the includes() method returns true because the result array contains the element ‘green’.

Check if an array contains a number

The following example shows how to use the includes() method to check if an array contains a number:

const ratings = [1,2,3,4,5];

let result = ratings.includes(4); 
console.log(result); // true

result = ratings.includes(6); 
console.log(result); // false

Check if an array contains an object

The following example uses the includes() method to check if an array contains an object:

const john = {
    'name': 'John Doe',
    'email': '[email protected]'
};
const jane = {
    'name': 'Jane Doe',
    'email': '[email protected]'
};

const list = [john, jane];
let result = list.includes(john);

console.log(result); // true

In this example, the list.includes(john) returns true because the list array contains the john object reference.

In practice, instead of searching for a reference, you often search for objects by their property values. The following example won’t work:

const list = [{
    'name': 'John Doe',
    'email': '[email protected]'
}, {
    'name': 'Jane Doe',
    'email': '[email protected]'
}];

let result = list.includes({
    'name': 'John Doe',
    'email': '[email protected]'
});

console.log(result); // false

In this example, the following object:

{
    'name': 'John Doe',
    'email': '[email protected]'
}

…looks like the first element in the list array. However, the includes() method returns false because the list doesn’t contain a reference to the searched object.

To check if an array contains an object, you follow these steps:

  • First, create a helper function that compares two objects by their properties.
  • Second, use the array.some() method to find the searched object by property values.

To compare objects by property values, you use the following helper function:

const isEqual = (first, second) => {
    return JSON.stringify(first) === JSON.stringify(second);
}

The isEqual() function returns true if the first and second objects have the same number of properties with the same values.

And the limitation of the isEqual() function is that the order of properties of the compared objects must be the same.

Note that some library provides the function that allows you to compare two objects by their property values.

For example, Lodash has a _.isEqual() method that allows you to compare objects if they are equal with better performance than using the JSON.stringify().

The following uses the array.some() method to match every element of an array with the searched object:

const result = list.some(e => isEqual(e, {
    'name': 'John Doe',
    'email': '[email protected]'
})); 

console.log(result); // true

Put it all together:

const list = [{
    'name': 'John Doe',
    'email': '[email protected]'
}, {
    'name': 'Jane Doe',
    'email': '[email protected]'
}];

const isEqual = (first, second) => {
    return JSON.stringify(first) === JSON.stringify(second);
}

const result = list.some(e => isEqual(e, {
    'name': 'John Doe',
    'email': '[email protected]'
}));

console.log(result); // true

Conclusion

  • For primitive values, use the array.includes() method to check if an array contains a value.
  • For objects, use the isEqual() helper function to compare objects and array.some() method to check if the array contains the object.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn.

Suggested Articles:

  1. How to Reverse an Array in JavaScript Using Different Methods

Leave a Comment