Last updated: Mar 3, 2024
Reading timeยท3 min
To check if multiple values exist in an array:
every()
method to iterate over the array of values.every
method will return true
.const arr = ['one', 'two', 'three', 'four']; const values = ['one', 'two']; const multipleExist = values.every(value => { return arr.includes(value); }); console.log(multipleExist); // ๐๏ธ true
The function we passed to the Array.every method gets called with each element until it returns a falsy value or iterates over the entire array.
On each iteration, we use Array.includes() method to check if the value is contained in the other array.
const arr = ['one', 'two', 'three', 'four']; console.log(arr.includes('one')); // ๐๏ธ true console.log(arr.includes('two')); // ๐๏ธ true console.log(arr.includes('abc')); // ๐๏ธ false
If even a single value is not contained in the array, the includes()
method
returns false
, which causes the every
method to short-circuit also returning
false
.
If you need to do this often, define a reusable function.
function multipleInArray(arr, values) { return values.every(value => { return arr.includes(value); }); } console.log(multipleInArray([1, 2, 3], [1, 3])); // ๐๏ธ true console.log(multipleInArray([1, 2, 3], [1, 10])); // ๐๏ธ false
The multipleInArray
function takes an array and a collection of values and
checks if the specified values exist in the array.
If the condition is met for all values, the function returns true
, otherwise,
false is returned.
Set
objectThis is a three-step process:
Set
object.every()
method to iterate over the array of values.Set.has()
method to check if each value is in the Set
.function multipleInArray(arr, values) { const set = new Set(arr); return values.every(value => { return set.has(value); }); } // ๐๏ธ true console.log( multipleInArray(['bobby', 'hadz', 'com'], ['bobby', 'hadz']), ); // ๐๏ธ false console.log(multipleInArray([1, 2, 3], [1, 10]));
We used the Set()
constructor to
convert the array to a Set
object to
remove all the duplicates (if any).
// ๐๏ธ Set(3) { 1, 2, 3 } console.log(new Set([1, 1, 2, 2, 3, 3]));
Set
objects are collections of unique elements, so if any duplicates exist in
the array, they get dropped once passed to the Set()
constructor.
The next step is to use the Array.every()
method to iterate over the array of
values.
On each iteration, we use the
Set.has()
method to check if the current value is contained in the Set
object.
The Set.has
method returns true
if the value is contained in the Set
and
false
otherwise.
indexOf()
This is a three-step process:
every()
method to iterate over the array of values.indexOf
method to check if each value is contained in the other
array.every
method will return true
.const arr = ['one', 'two', 'three', 'four']; const values = ['one', 'two']; const multipleInArray = values.every(value => { return arr.indexOf(value) !== -1; }); console.log(multipleInArray); // ๐๏ธ true
This is very similar to our previous example. However, this time we used the Array.indexOf method.
indexOf
method checks if a value is contained in an array and returns the index
of the first occurrence of the value in the array.If the value is not contained in the array, the method returns -1
.
If the indexOf
method doesn't return -1
, then the value is contained in the
array.
Here is a reusable function that achieves the same result.
function multipleInArray(arr, values) { return values.every(value => { return arr.indexOf(value) !== -1; }); } // ๐๏ธ true console.log(multipleInArray([1, 2, 3], [1, 2])); // ๐๏ธ false console.log(multipleInArray([1, 2, 3], [4, 2]));
The function takes the array and the collection of values as parameters and
returns true
if all of the values are contained in the array and false
otherwise.
You can learn more about the related topics by checking out the following tutorials: