-1

Welcome,
I need to find whether array contains array
I tried two ways(indexOf and includes),but both returns a negative result.

let myarray = [
  [4, 2],
  [2, 2],
  [2, 2],
  [2, 4],
  [2, 2],
];
console.log(myarray.includes([4, 2]));//Returns false
console.log(myarray.indexOf([4, 2]));//Returns -1

I then thought of having a foreach loop,and checking the array(using ==),
But for that even console.log([4, 2]==[4, 2]);//Returns false
Please Help-I know this is simple for you,

1 Answers1

0

Use some and Array.isArray. some will return true if any one condition in the callback function is true and use Array.isArray to check if the element is an array

let myarray = [
  [4, 2],
  [2, 2],
  [2, 2],
  [2, 4],
  [2, 2],
];

const hasNestedArray = myarray.some(elem => Array.isArray(elem));
console.log(hasNestedArray)
brk
  • 48,835
  • 10
  • 56
  • 78