这是一个tic tac toe游戏应用程序的尝试。 我有两个阵列playerMoves
和winningCombinations
。喜欢这个。
如何将数组与数组进行比较?
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]
];
我需要将winningCombination
阵列,使得在-至少和在最playerMoves
阵列的两个值与winningCombination
每个阵列匹配滤波器。
findPossibleMove(playerMoves);
// should return [[0,1,2],[1,4,7], [0,4,8] ]
我尝试
function findPossibleMove(arr){
var found = 0;
return arr.forEach((item)=>{
winningCombinations.map((obj)=>{
if(obj.indexOf(item) !== -1) {
found++;
}
if(found===2){
return obj;
}
})
})
}