There’re a few ways to check if an array contains an item in JavaScript.
We can use the includes
, some
, filter
, find
, findIndex
, or indexOf
to do the check. All methods are part of the array instance.
includes
The includes
method takes in a value and returns true
if it’s in the array or false
otherwise. The check is done with ===
operator.
For instance, we can call includes
as follows:
const arr = [1, 2, 3];
const result = arr.includes(1);
In the code above, we called includes
on arr
and then check if 1 is in it.
Therefore, it should return true
so result
is true
.
some
The some
method takes a callback with the condition of the item to check for. We can use it as follows:
const arr = [1, 2, 3];
const result = arr.some(a => a === 1);
In the code above, we passed in a callback to the some
method to check if any array entry, which is the value of a
is 1.
It returns true
if it’s in the array and false
otherwise, so we should get the same result.
filter
The filter
method returns and array with the items that meet the condition returned by the callback included.
For instance, we can call it as follows:
const arr = [1, 2, 3];
const result = arr.filter(a => a === 1).length > 0;
In the code above, we called the filter
method with the same callback, which returns an array with 1 in it. Then we get the length
property of it and the make sure it’s bigger than 0.
Therefore, we get the same result as before by calling filter
and adding the length
check.
find
The find
method returns the first entry in the array that meets the given condition returned in the callback.
If the item isn’t found, then undefined
is returned.
For instance, we can write the following:
const arr = [1, 2, 3];
const result = typeof arr.find(a => a === 1) !== 'undefined'
In the code above, we use the typeof
operator to check if the array item returned by find
is undefined
or not. If it’s not undefined
, then it’s found. Otherwise, it’s not.
Then we should get the same result as before.
findIndex
The findIndex
method returns the index of the first element in the array that meets the given condition returned in the callback.
If the item isn’t found, then -1 is returned.
For instance, we can write the following code to use findIndex
:
const arr = [1, 2, 3];
const result = arr.findIndex(a => a === 1) !== -1;
In the code above, we called findIndex
with the same callback used in the previous examples to search if any item in the array is 1. Then we check if the returned index isn’t -1.
Therefore, it should return the same boolean result as before, which is true
.
indexOf
indexOf
returns the index of the item that matches the value passed in as the argument.
It returns -1 if the value isn’t found.
For instance, we can use it as follows:
const arr = [1, 2, 3];
const result = arr.indexOf(1) !== -1;
We checked the returned array against -1 so that we can check if it’s in the array.
So we should get the same results as before.
some
, find
and findIndex
are suitable for searching for any object, while the rest are suitable for primitive values since they let search with any condition instead of just letting us pass in a value or variable.
Objects are compared with the ===
operator so it doesn’t compare the structure of an object.