0

These codes don't work:

const a = [[1, 1], [2, 2]]
console.log(a.includes([1, 1])); // --> false
console.log(a.indexOf([1, 1])); // --> -1

This work but I think its not optimized

console.log(a.map(x => x.toString()).includes([1, 1].toString()));
// --> true

Is there a simpler way ?

Vincent
  • 45
  • 6

2 Answers2

0

const a = [[1, 1], [2, 2]]
var index=a.findIndex(x=>{return JSON.stringify(x)===JSON.stringify([2, 2])})

console.log(`item index : ${index}`);
Libin C Jacob
  • 1,108
  • 12
  • 34
-2

Assuming this:

var arr = ['a', 'b', 'b'];

you can invoke:

Array.isArray(arr);

will return true if the considered variable is an array, otherwise not.

Once you get it, you can apply it to the external array.

Istorn
  • 485
  • 1
  • 5
  • 23