admin管理员组

文章数量:1415145

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 binations.

There are 8 winning binations.

Each winning bination 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 e 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)

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 binations.

There are 8 winning binations.

Each winning bination 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 e 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?

Share Improve this question edited May 23, 2017 at 12:30 CommunityBot 11 silver badge asked Nov 11, 2016 at 17:58 nikjohnnikjohn 22k15 gold badges56 silver badges88 bronze badges 5
  • 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 Commented Nov 11, 2016 at 18:03
  • I undertand board and winningCombos but trouble figuring out what exactly you want to pute. Are you trying to figure out if score exists in win? – skav Commented Nov 11, 2016 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 binations that would win, when their values are 1. I'll add this to the question as well – nikjohn Commented Nov 11, 2016 at 18:08
  • @skav Yes. In a nutshell, yes. I'll add that to the question as well – nikjohn Commented Nov 11, 2016 at 18:10
  • 1 good answers below - but if you want a general routine for array equality, see this: stackoverflow./questions/7837456/… – skav Commented Nov 11, 2016 at 18:19
Add a ment  | 

2 Answers 2

Reset to default 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);

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

本文标签: Javascript Search for an array in an array of arraysStack Overflow