-1

Hi I want to check if "arr" includes one of the sub arrays of "arrNested" and get a boolean value. I want something like arrNested.includes(arr) that works for nested arrays console.log(arrNested.includes(arr)) returns false and I don't know why

let arr = [1,2,3]
let arrNested = [
    [1,2,3],
    [4,5,6],
    [7,8,9],
]
  • 1
    What is preventing you from writing this? – Scott Hunter Jul 18 '21 at 20:58
  • 1
    Does `[1, 2, 3] == [3, 2, 1]`? – Justinas Jul 18 '21 at 20:58
  • _“`console.log(arrNested.includes(arr))` returns `false` and I don’t know why”_ — [duplicate](//google.com/search?q=site%3Astackoverflow.com+js+includes+with+nested+array+returns+false) of [Why can't I use Array#includes for a nested array?](/q/48088657/4642212). – Sebastian Simon Jul 18 '21 at 21:57

1 Answers1

-1

Use Array.some().

If the order matters, use:

let arr = [1, 2, 3]
let arrNested = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
]

function includes(a, b) {
  return a.some(e => e.toString() == b.toString());
}
console.log(includes(arrNested, arr));

If the order does not matter, first sort, then compare:

let arr = [1, 2, 3]
let arrNested = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
]

function includes(a, b) {
  var s = b.sort().toString();
  return a.some(e => e.sort().toString() == s);
}
console.log(includes(arrNested, arr));
Spectric
  • 30,714
  • 6
  • 20
  • 43