Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare an array to an array of arrays?

This is an attempt in a tic tac toe game app. I have two arrays playerMoves and winningCombinations. Like this.

var playerMoves= [0,1,4];
var winningCombinations = [
        [0,1,2],[3,4,5],[6,7,8],
        [0,3,6],[1,4,7],[2,5,8],
        [0,4,8],[2,4,6]
      ];

I need to filter the winningCombination array such that at-least and at-most two values of playerMoves array matches with each array in winningCombination.

findPossibleMove(playerMoves);
// should return [[0,1,2],[1,4,7], [0,4,8] ]

My attempt

function findPossibleMove(arr){
  var found = 0;
  return arr.forEach((item)=>{
    winningCombinations.map((obj)=>{
      if(obj.indexOf(item) !== -1) {
        found++;
      }
      if(found===2){
        return obj;
      }        
    })
  })      
}
like image 472
anoop chandran Avatar asked Jul 17 '17 19:07

anoop chandran


People also ask

How do I compare two arrays of arrays?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.

How do you compare two data arrays?

A simple way is to run a loop and compare elements one by one. Java provides a direct method Arrays. equals() to compare two arrays. Actually, there is a list of equals() methods in the Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is the base of all classes in Java).

How do you compare if 2 arrays are equal to each other?

To check if two arrays are equal or not, we have to compare the exact occurrence of each of the elements in both of the arrays to be the same. However, the problem is that the values of the arrays could be in any permutation irrespective of each other.


2 Answers

Three simple steps:

  • Use indexOf function to check, if specified element from the subarray of winningCombinations array is present in the playerMoves array.
  • If so - filter it out with Array#filter function.
  • If the returned, filtered subarray has length equal to 2, it means that two (no more, nor less) elements have appeared - it fulfills our condition - filter it once again with yet another Array#filter.

let playerMoves = [0, 1, 4];
let winningCombinations = [
  [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 res = winningCombinations.filter(v => v.filter(c => {
  return playerMoves.indexOf(c) > -1;
}).length == 2);
  
  console.log(JSON.stringify(res));
like image 67
kind user Avatar answered Sep 22 '22 21:09

kind user


You can use filter and includes to achieve that:

var playerMoves= [0,1,4];
var winningCombinations = [
  [0,1,2],[3,4,5],[6,7,8],
  [0,3,6],[1,4,7],[2,5,8],
  [0,4,8],[2,4,6]
];

var filteredCombinations = winningCombinations.filter((combination) =>
  combination.filter(x => playerMoves.includes(x)).length === 2);

console.log(filteredCombinations);
like image 21
Alberto Trindade Tavares Avatar answered Sep 22 '22 21:09

Alberto Trindade Tavares