How to Check if an Array Contains a Value in Javascript

Summary: in this tutorial, you’ll learn how to check if an array contains a value in JavaScript.

A value in JavaScript can be primitive such as a number or string. Or it can be an object.

This tutorial shows you how to check if an array contains a value, being a primitive value or object.

1) 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); // trueCode language: JavaScript (javascript)

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); // trueCode language: JavaScript (javascript)

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'.

2) 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); // falseCode language: JavaScript (javascript)

3) 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); // trueCode language: JavaScript (javascript)

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); // falseCode language: JavaScript (javascript)

In this example, the following object:

{
    'name': 'John Doe',
    'email': '[email protected]'
}Code language: JavaScript (javascript)

…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);
}Code language: JavaScript (javascript)

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 a 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 a 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); // trueCode language: JavaScript (javascript)

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); // trueCode language: JavaScript (javascript)

Summary

  • 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.
Was this tutorial helpful ?