1

this might be a duplicate but I couldn't find a solution. If I have an array of arrays, is there a "built-in" way to determine if that array of arrays includes another array. Array.prototype.includes() helps, but doesn't get me all the way because of (I think) object references as shown below. I can manually test equality of each value but I'm sure there's a better way.

$ node
> b = [[1,2,3]]
[ [ 1, 2, 3 ] ]
> b[0]
[ 1, 2, 3 ]
> b.includes(b[0])
true
> b.includes([1,2,3])
false
> 
jboxxx
  • 707
  • 10
  • 19

1 Answers1

3

I recommend to use JSON.stringify & includes.

JSON.stringify([[1,2,3]]).includes(JSON.stringify([1,2,3]))
true
JSON.stringify([[1,3]]).includes(JSON.stringify([1,2,3]))
false
seunggabi
  • 1,699
  • 12
  • 12
  • While this may be a quick and dirty solution, this has some pitfalls. To give an example: `JSON.stringify([[_ => 0]]).includes(JSON.stringify([null]))` – ASDFGerte Dec 28 '19 at 16:00
  • @ASDFGerte you are right, but it's only a case where the array contains functions. It's safe to use if he can assume the array contains only numbers. – Omri Attiya Dec 28 '19 at 16:33