Search code examples
javascriptarraysecmascript-6include

Check if an array of arrays contains a value in javascript


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']]

Solution

  • 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