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:
Approaches:
Approach 1: Using JavaScript array.every() method
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(); |
Array_2 is the subset of Array_1
Approach 2: Using JavaScript array.some() method
This approach declares false if some element of the second array is not present in the first array.
Example: This example uses the approach discussed above.Â
Javascript
// Create input arrays let arr1 = [ "GFG" , "Geeks" , "Portal" , Â Â Â Â Â Â Â Â Â Â Â Â "Geek" , "GFG_1" , "GFG_2" ]; Â
let arr2 = [ "GFG" , "Geeks" , "GeekForGeeks" ]; Â
// Function to check subarrays function GFG_Fun() {     let res = !arr2.some(val => arr1.indexOf(val) === -1);     let not = "" ;     if (!res) {         not = "not" ;     }          // Display output     console.log( "Array_2 is " + not         + " the subset of Array_1" ); } Â
GFG_Fun(); |
Array_2 is not the subset of Array_1