Are you wondering how to check if a JavaScript array includes multiple values?
Just keep reading, as we will show you the solutions on how you can do it simply yet effectively.
But before we talk about how to check for multiple values that exist in an array in JavaScript, let’s quickly go over what an array is in JavaScript.
What is Array in JavaScript?
An array is a way to store many values in one or single variable.
In JavaScript, arrays start counting from zero, so the first value is at index 0, the second value is at index 1, and so on.
What is includes() method?
The includes() method is a built-in function in JavaScript that determines whether an array includes a certain value among its entries.
The includes() method only checks for the presence of a single value in an array.
Here’s the syntax of includes() method:
array.includes(searchElement[, fromIndex])
📌searchElement
The value to search for.
📌fromIndex (optional)
The position in the array at which to begin searching for searchElement.
A negative value searches from the index of array.length + fromIndex by asc. Defaults to 0.
Moreover, the includes() method in JavaScript for arrays will return true if a value is present in the array, and false if it is not.
For example:
let sample = ["hi", "welcome to", "itsourcecode"];
let sample2 = "itsourcecode";
let result = sample.includes(sample2);
console.log(result);
In this example, we have an array sample that contains the values “hi”, “welcome to”, and “itsourcecode.”
We also have a variable sample2 that contains the value “itsourcecode.”
We use the includes() method to check if the sample array includes the value of sample2, which is “itsourcecode.”
The result is true because “itsourcecode” is included in the sample array.
When you log the value of result to the console, it outputs true.
Output:
true
How to use includes() to check multiple values exist in an array in JavaScript?
As mentioned previously, the includes() method in JavaScript is designed to solely check for the existence of a single value within an array.
If you want to check for the presence of multiple values includes in an array in JavaScript, you can use the every() method in combination with the includes() method.
Here’s an example that shows how to use the every() method to check if an array includes all of the selected items
const samplearr= [10, 20, 30, 40, 50];
const selectedItems = [10, 20, 50];
const result = selectedItems.every(item => arr.includes(item));
console.log(result);
In this example, we use the every() method to check if every item in the selectedItems array is included in the samplearr array.
The result is true because both 10, 20, and 30 are included in the samplearr array.
Output:
true
Solutions on how to check JavaScript array includes multiple values?
To check if multiple values exist in array in JavaScript, here’s the solutions you need.
Aside from using every() method that we illustrate above, here are the different ways to check if an array includes multiple values in JavaScript.
Solution 1: Use the filter() method
Here’s the example code using the filter() method in combination with the includes() method:
const samplearr = ["It", "source", "code"];
const selectedItems = ["It", "code"];
const result = selectedItems.filter(item => samplearr.includes(item)).length === selectedItems.length;
console.log(result);
In this solution, we use the filter() method to create a new array that contains only the items from the selectedItems array which are also present in the arr array.
The filter() method checks each item in the selectedItems array using the includes() method to see if it exists in the samplearr array.
If all items from selectedItems are found in samplearr, the length of the new array will be the same as the length of selectedItems, indicating that all items are included.
Output:
true
Solution 2: Use for loop
Here’s the example solution using for loop.
const samplearr = [100, 200, 300, 400, 500];
const selectedItems = [100, 200, 500];
let result = true;
for (let i = 0; i < selectedItems.length; i++) {
if (!samplearr.includes(selectedItems[i])) {
result = false;
break;
}
}
console.log(result);
In this solution, we use a for loop to go through each item in the selectedItems array. We check if each item is present in the samplearr array using the includes() method.
If we find any item that is not in the samplearr array, we set the result as false and stop the loop.
This means that not all of the items in the selectedItems array are included in the arr array.
Output:
true
Here’s the example code, if we input item that is not in the samplearr array:
const samplearr = [100, 200, 300, 400, 500];
const selectedItems = [10, 20, 50];
let result = true;
for (let i = 0; i < selectedItems.length; i++) {
if (!samplearr.includes(selectedItems[i])) {
result = false;
break;
}
}
console.log(result);
Output:
false
Conclusion
In conclusion, this article provided several solutions on how to check if a JavaScript array includes multiple values.
The includes() method in JavaScript can be used to check the presence of a single value in an array, returning true if the value is found and false if it is not.
The every() method can be combined with includes(). To check for the existence of multiple values exist in array in JavaScript.
Additionally, the filter() method can be utilized to create a new array containing only the items that are present in both arrays, and a for loop can be employed to iterate through the selected items and check their inclusion in the array using includes().
These methods offer flexible approaches to efficiently verify the presence of multiple values in a JavaScript array.
We are hoping that this article provides you with enough information that helps you understand JavaScript array includes multiple values.
You can also check out the following article:
Thank you for reading itsourcecoders 😊.