There is a multidimensional array in javascript:

let win_tbl = [ [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6] ]; 

How to check if this array contains an array inside:

 let myhodarr = [1,4,7]; 

Check:

 if(win_tbl.indexOf(myhodarr) !== -1) 

does not work.

  • F. Tomas, if you are satisfied with my answer, then put a tick next to the answer on the left, please. - Bharata

3 answers 3

 let win_tbl = [ [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6] ]; let myhodarr = [1,4,7]; console.log(JSON.stringify(win_tbl).indexOf(JSON.stringify(myhodarr)) != -1); 

  • one
    an interesting decision!) Straight awakens a feeling of admiration) - Dmytryk
  • @ Dmytryk I am pleased that you liked it :). - Igor

 let win_tbl = [ [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6] ]; let myhodarr = [1,4,7]; var found = win_tbl.filter(function (e) { return e.length === myhodarr.length && e.every(function (v, i) { return v === myhodarr[i]; }); }) console.log(found); 

    See explanation below:

     var win_tbl = [ [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6] ], myhodarr = [1,4,7]; console.log(win_tbl.join('_').indexOf(myhodarr.join()) > -1); console.log(win_tbl.join('_').indexOf('' + myhodarr) > -1); console.log(win_tbl.join('_').indexOf([6,1,4].join()) > -1); console.log(win_tbl.join('_').indexOf('' + [6,1,4]) > -1); 

    How it works:

    Calling win_tbl.join('_') gives us the following string:

    0,1,2_3,4,5_6,7,8_0,3,6_1,4,7_2,5,8_0,4,8_2,4,6

    Calling myhodarr.join() gives us the following string:

    1,4,7

    Next, using the line function indexOf() we look for the 2nd line in the 1st. If it is there, then the array in the array also exists.

    Tip: the expression myhodarr.join() can be replaced by '' + myhodarr and we get the same thing, but in short.

    Help: Array.join ()