0

I know that if there is an array of values it must be used this approach:

console.log(['joe', 'jane', 'mary'].includes('jane')); // true

But in case of an array of arrays, is there a short way to do it? Without other computations between.

For this input:

[['jane'],['joe'],['mary']]
1
  • 5
    arr.some((a) => a.includes("jane")); Commented Jun 1, 2021 at 9:47

3 Answers 3

8

You can use flat method to flatten the array. For more neted array, you can also mention depth like flat(depth)

let arr = [["jane"],["joe"],["mary"]];

arr.flat().includes('jane'); //true
Sign up to request clarification or add additional context in comments.

Comments

4

You can easily achieve this result using some

arr.some((a) => a.includes("jane"))

const arr = [
  ["jane"],
  ["joe"],
  ["mary"]
];
const arr2 = [
  ["joe"],
  ["mary"]
];

console.log(arr.some((a) => a.includes("jane")));
console.log(arr2.some((a) => a.includes("jane")));

Comments

0

it can also be done by first flattening the 2d arrays in 1 d aaray and then using includes to find whether the array contains the element or not

var arr = [['jane'],['joe'],['marry']]
var newarr=[].concat(...arr)
var v=newarr.includes('jane')
console.log(v)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.