How To Check If Multiple Values Exist In Array In JavaScript

Check if Multiple Values exist in Array in JavaScript

Today, I will show you three methods to check if multiple values exist in array in JavaScript: Array.every(), Array.includes() and Array.indexOf(). Do you know all these methods and how to apply them? If not, please follow this article to understand more details.

Check if multiple values exist in array in JavaScript

We have two arrays that is color and myFavorite array as follows:

let color = ['red', 'blue', 'yellow', 'white', 'pink', 'green'];
let myFavorite = ['white', 'green', 'blue'];

Now we want to check if all the values ​​of the myFavorite array are in the color array or not. Follow the following two ways.

Using Array.every() and Array.indexOf() methods

The idea for checking all values ​​of array myFavorite exists in array color is that we would check each value of array myFavorite in turn color array or not. If all values ​​are contained in that array, it returns true. If any value is not in that array, it returns false.

We will use the indexOf() function to check the value. It will search for an element in the array based on the element’s value. If found, the function will return the element’s value (key) and return - 1 if not found. If all returns are different from -1, then every value in the myFavorite array exists in the color array.

To iterate through all the values ​​in the array and check if the values ​​all returned results according to the condition, we use the every() function.

Every () function is used to help check if all elements in the array satisfy a specific condition. If all elements are satisfied, it will return true; otherwise, if only one element is not satisfied, it will return false.

Code sample

let color = ['red', 'blue', 'yellow', 'white', 'pink', 'green'];
let myFavorite = ['white', 'green', 'blue'];
 
// Use every() function to iterate over the elements 
// and indexOf() function to return the index of the first found element
let existValue = myFavorite.every(value =>{
    return color.indexOf(value) !== -1;
})
 
console.log(existValue); // true

If any value in the myFavorite array is not found in the color array, then the indexOf() function will return -1, and the every() function will return false.

Using Array.every() and Array.includes() methods

If you want the search result to return true or false but not the position of the first occurrence of the element found, we can use the includes() function in Javascript. The includes() method is used to find if an element exists in the array and returns Boolean data. If an element is found in the array, it will return true. Otherwise, return false.

Code sample

let color = ['red', 'blue', 'yellow', 'white', 'pink', 'green'];
let myFavorite = ['white', 'green', 'blue'];
 
// Use every() function to iterate over the elements 
// and includes() function to look for value in the array and return Boolean
let existValue = myFavorite.every(value =>{
    return color.includes(value);
})
 
console.log(existValue); //true

If any value in the myFavorite array is not found in the color array, then the includes() function will return false, and the every() function will return false.

Summary

Above, I have introduced two methods to help you check if multiple values exist in array in JavaScript. However, note that Internet Explorer does not support the includes() function, so if you are in this situation, it is recommended to use the indexOf() method. Hope this article helps you. Good luck with your practice.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *