1

I have 2 arrays, where arr1 is a subset of arr2, but I am unable to check this using Javascript.

arr1 = [1,2]; //array1
arr2 = [[1,2],[2,3],[1,3],[1,4]]; //array2

I tried achieving this by using arr2.includes(arr1) which always returns false.

let arr1 = [1,2];
let arr2 = [[1,2],[2,3],[1,3],[1,4]];

console.log(arr2.includes(arr1)); //getting false

I expect the output to be true, but the actual output is false. Can anyone help me solve this problem and direct me whether this is the right approach to solve this problem or not?

D Malan
  • 10,272
  • 3
  • 25
  • 50

1 Answers1

0

Use JSON.stringify and compare with the individual arrays in arr2

var arr1 = [1,2]; 
var arr2 = [[1,2],[2,3],[1,3],[1,4]];
arr2.forEach(function(e){
JSON.stringify(e)==JSON.stringify(arr1)?console.log(true):false})
ellipsis
  • 12,049
  • 2
  • 17
  • 33