简体   繁体   English

在 arrays 的另一个数组中搜索一个数组的元素

[英]search for elements of one array in another array of arrays

I have this two array: linesToWin and mosseX: [].我有这两个数组:linesToWin 和 mosseX:[]。 A possible configuration of mosseX is: mosseX[1,3,4,7]. mosseX 的一个可能配置是:mosseX[1,3,4,7]。 I want to control if there are three number in mosseX that are equal to an array of linesToWin and return true or false dipending by the searching.我想控制 mosseX 中是否有三个等于 linesToWin 数组的数字,并通过搜索返回 true 或 false 。 Someone could help me?有人可以帮助我吗?

calculateWinner = () => {
  const linesToWin = [
    [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 result = false;
  for (let i = 0; i < linesToWin.length; i++) {
    result = this.state.mosseX.filter((mossa) =>
      mossa.includes(linesToWin[i].map((u) => u))
    );
    if (result === true) return result;
  }
  return result;
};

You can simply contact array and compare.您可以简单地联系数组并进行比较。

Sample:样本:

calculateWinner = () => {
  const linesToWin = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  function merge(data = []) {
    return data.join("-");
  }
  const mosseX = merge(this.state.mosseX);
  return linesToWin.some((line) => merge(line) === mosseX);
};

For testing:用于检测:

 calculateWinner = (mosseX) => { const linesToWin = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6], ]; function merge(data = []) { return data.join("-"); } // For testing // const mosseX = merge(this.state.mosseX); mosseX = merge(mosseX); return linesToWin.some((line) => merge(line) === mosseX); }; console.log(calculateWinner([6,7,1])) console.log(calculateWinner([6,7,8])) console.log(calculateWinner([1,7,8]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM