0
const a = [[1,1], [2,1], [3,1]];
console.log(a);
console.log(a.includes([1,1]));

> false

Can someone explain to me why the above outputs false? For what its worth, this occurs in any of the search functions (.find, .indexof, etc), as well as if I try [1,1] in a. I'm clearly missing something about how multidimensional array searching works in javascript.

eykanal
  • 26,437
  • 19
  • 82
  • 113
  • Comparison of objects is done on the basis of memory location and not their value, iirc. That is, the only way the two objects will be equal is if they are the exact same object. For instance, `var a={}, b={}; console.log(a==b); var c=a; console.log(a==c);` And since arrays are objects, `var a=[], b=[]; console.log(a==b); var c=a; console.log(a==c);` produces the same result. – Chris Strickland Dec 22 '21 at 03:24
  • See this question for discussion: https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects – Chris Strickland Dec 22 '21 at 03:26

1 Answers1

1

This is basically how equal to operator works in JavaScript. If we try to compare two arrays

[1]=== [1] // false equality operator can compare primitive values(number,string Boolean). But in case of arrays and objects it compares for reference. So here we are comparing two different references so it is coming as false. For same reason you are getting false. You have to write customer logic to compare arrays.

Amit
  • 3,662
  • 2
  • 26
  • 34