-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],
]
3
  • 1
    What is preventing you from writing this? Commented Jul 18, 2021 at 20:58
  • 1
    Does [1, 2, 3] == [3, 2, 1]? Commented Jul 18, 2021 at 20:58
  • console.log(arrNested.includes(arr)) returns false and I don’t know why” — duplicate of Why can't I use Array#includes for a nested array?. Commented Jul 18, 2021 at 21:57

1 Answer 1

-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));

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.