How to useJavaScript array.every() method in Javascript
This approach checks whether every element of the second array is present in the first array or not.
Example: This example uses the approach discussed above.
Javascript
// Creating 2 arrays let arr1 = [ "GFG" , "Geeks" , "Portal" , "Geek" , "GFG_1" , "GFG_2" ]; let arr2 = [ "GFG" , "Geeks" , "Geek" ]; // Funtion to check subarray function GFG_Fun() { let res = arr2.every( function (val) { return arr1.indexOf(val) >= 0; }); let not = "" ; if (!res) { not = "not" ; } // Display the output console.log( "Array_2 is " + not + " the subset of Array_1" ); } GFG_Fun(); |
Output
Array_2 is the subset of Array_1
How to check whether an array is subset of another array using JavaScript ?
The task is to check whether an array is a subset of another array with the help of JavaScript. There are a few approaches, we will discuss below: