How to compare an array to an array of arrays?

anoop chandran

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;
      }        
    })
  })      
}
kind user

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));

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

PHP - How to remove duplicate values from array (compare 2 arrays)

分類Dev

How to insert an array into an array of arrays?

分類Dev

Can I compare the equality of an array with an ArrayList of arrays?

分類Dev

How to insert a numpy array to a numpy array of arrays?

分類Dev

How to convert an array of arrays into a matrix?

分類Dev

Compare an array with an array of objects

分類Dev

compare values in arrays and if any values match, increment values in first array

分類Dev

merge array with array of arrays

分類Dev

How to compare two object elements in a mongodb array

分類Dev

How to compare string to String Array in C#?

分類Dev

How to compare two array and alert the result?

分類Dev

How to compare each element of array to other elements?

分類Dev

How to write filter that inspects an array of arrays

分類Dev

How to check bordering duplicates in an array of arrays

分類Dev

How to count items in arrays of values in an array of hashes

分類Dev

How to count occurrences of strings in array of arrays?

分類Dev

How to array_unique dimensional arrays

分類Dev

How to send array of arrays as data in $.ajax()?

分類Dev

how to combine two arrays on the basis of first array

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

how to make single deep array out of couple arrays deep array

分類Dev

How to filter arrays depending on the values of arrays inside the array?

分類Dev

Javascript array value compare

分類Dev

Convert array of objects to array of arrays

分類Dev

How can I compare a loop value to an array value that is not sequentially the same?

分類Dev

How to implement Array.prototype.sort default compare function?

分類Dev

Laravel, How to get array of session in controller and compare with model

分類Dev

How to i compare the value in array for similar regardless the order of index

分類Dev

How can I convert an array of arrays to an array of objects, selecting the key from other array?

Related 関連記事

  1. 1

    PHP - How to remove duplicate values from array (compare 2 arrays)

  2. 2

    How to insert an array into an array of arrays?

  3. 3

    Can I compare the equality of an array with an ArrayList of arrays?

  4. 4

    How to insert a numpy array to a numpy array of arrays?

  5. 5

    How to convert an array of arrays into a matrix?

  6. 6

    Compare an array with an array of objects

  7. 7

    compare values in arrays and if any values match, increment values in first array

  8. 8

    merge array with array of arrays

  9. 9

    How to compare two object elements in a mongodb array

  10. 10

    How to compare string to String Array in C#?

  11. 11

    How to compare two array and alert the result?

  12. 12

    How to compare each element of array to other elements?

  13. 13

    How to write filter that inspects an array of arrays

  14. 14

    How to check bordering duplicates in an array of arrays

  15. 15

    How to count items in arrays of values in an array of hashes

  16. 16

    How to count occurrences of strings in array of arrays?

  17. 17

    How to array_unique dimensional arrays

  18. 18

    How to send array of arrays as data in $.ajax()?

  19. 19

    how to combine two arrays on the basis of first array

  20. 20

    How to get the combination of array values from nested arrays in an array of objects

  21. 21

    how to make single deep array out of couple arrays deep array

  22. 22

    How to filter arrays depending on the values of arrays inside the array?

  23. 23

    Javascript array value compare

  24. 24

    Convert array of objects to array of arrays

  25. 25

    How can I compare a loop value to an array value that is not sequentially the same?

  26. 26

    How to implement Array.prototype.sort default compare function?

  27. 27

    Laravel, How to get array of session in controller and compare with model

  28. 28

    How to i compare the value in array for similar regardless the order of index

  29. 29

    How can I convert an array of arrays to an array of objects, selecting the key from other array?

ホットタグ

アーカイブ