0

I am looking at the best way to search for an instance of an array containing the elements of a given array, in an array of arrays.

Now, I understand that that's a confusing line. So here's an example to illustrate the scenario.

I have a search set which is an array with 9 items, representing a game board of 9 cells. The values can be 1, 0 or null:

var board = [1, 0, 1, 1, 0, 1, 0, 0, null];

I also have a result set, which is an array of arrays:

var winningCombos = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]

Each array in winningCombo represents indices in the board array, that are winning combinations.

There are 8 winning combinations.

Each winning combination is a group of 3 indices, that would win, if their values are all 1.

i.e. to win, the board could be:

board = [1,1,1,0,0,0,null,null,0]; // Index 0,1, and 2 are 1, matching winningCombos[0]

or

board = [null,null,1,0,1,0,1,null,0]; // Index 2,4, and 6 are 1, matching winningCombos[7]

My question is:

What is the way in Javascript to perform this operation (maybe with ES6)?

What I have come up with so far is this:

const win = [[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 board = [null,null,1,0,1,0,1,null,0];

let score = [];

board.forEach(function(cell, index) 
    {
      if(cell === 1) 
        score.push(index);
});
console.log(score);
console.log(win.indexOf(score) > -1)

But I'm having a tough time finding the array in the array of arrays. Although the score is [2,4,6] and this exact array exists in win, it doesn't show up in the result, because of the way object equality works in Javascript I assume.

In a nutshell, I'm trying to see if score exists in win

I found this solution, but it seems quite hacky. Is there a better way to handle this?

Community
  • 1
  • 1
nikjohn
  • 20,026
  • 14
  • 50
  • 86
  • How does `board` array correspond to `win` array? Why are there eight indexes at `win`, having `.length` of `8` and nine indexes at `board`, having `.length` of `9`? – guest271314 Nov 11 '16 at 18:03
  • I undertand board and winningCombos but trouble figuring out what exactly you want to compute. Are you trying to figure out if score exists in win? – skav Nov 11 '16 at 18:04
  • @guest271314: the board array is an array of 9 cells. The win array is an array of indixes of all possible 3 cell combinations that would win, when their values are 1. I'll add this to the question as well – nikjohn Nov 11 '16 at 18:08
  • @skav Yes. In a nutshell, yes. I'll add that to the question as well – nikjohn Nov 11 '16 at 18:10
  • 1
    good answers below - but if you want a general routine for array equality, see this: http://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript – skav Nov 11 '16 at 18:19

2 Answers2

4

You can use Array.prototype.some(), Array.prototype.every() to check each element of win, score

const win = [
  [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 board = [null, null, 1, 0, 1, 0, 1, null, 0];

let score = [];

board.forEach(function(cell, index) {
  if (cell === 1)
    score.push(index);
});
console.log(score);
let bool = win.some(function(arr) {
  return arr.every(function(prop, index) {
    return score[index] === prop
  })
});
console.log(bool);
guest271314
  • 1
  • 15
  • 104
  • 177
1

Using ES6 you can map the win array to the actual values at each one of those locations:

const win = [[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 board = [null,null,1,0,1,0,1,null,0];
let winning_spots = win.map((spots) => spots.map((i) => board[i]));
>>> winning_spots
[[null, null, 1], [0, 1, 0], [1, null, 0], [null, 0, 1], [null, 1, null], [1, 0, 0], [null, 1, 0], [1, 1, 1]]

Then we can filter by which ones have all of either 1's or 0's:

let one_winners = winning_spots.filter((spots) => spots.every((e) => e == 1));
let zero_winners = winning_spots.filter((spots) => spots.every((e) => e == 0));
>>> one_winners
[[1, 1, 1]]
>>> zero_winners
[]

Finally, if we want to find whether there is a winner, just check the lengths:

let is_winner = (one_winners.length + zero_winners.length) > 0
Adam Van Prooyen
  • 891
  • 7
  • 17