如何检查数组是否是 JavaScript 中另一个数组的子集?

IT技术 javascript arrays
2021-02-03 02:52:11

假设我有两个数组,

var PlayerOne = ['B', 'C', 'A', 'D'];
var PlayerTwo = ['D', 'C'];

使用javascript检查arrayTwo是否是arrayOne的子集的最佳方法是什么?

原因:我试图理清一个游戏井字游戏的基本逻辑,结果卡在了中间。无论如何,这是我的代码......谢谢!

var TicTacToe = {


  PlayerOne: ['D','A', 'B', 'C'],
  PlayerTwo: [],

  WinOptions: {
      WinOne: ['A', 'B', 'C'],
      WinTwo: ['A', 'D', 'G'],
      WinThree: ['G', 'H', 'I'],
      WinFour: ['C', 'F', 'I'],
      WinFive: ['B', 'E', 'H'],
      WinSix: ['D', 'E', 'F'],
      WinSeven: ['A', 'E', 'I'],
      WinEight: ['C', 'E', 'G']
  },

  WinTicTacToe: function(){

    var WinOptions = this.WinOptions;
    var PlayerOne = this.PlayerOne;
    var PlayerTwo = this.PlayerTwo;
    var Win = [];

    for (var key in WinOptions) {
      var EachWinOptions = WinOptions[key];

        for (var i = 0; i < EachWinOptions.length; i++) {
          if (PlayerOne.includes(EachWinOptions[i])) {
            (got stuck here...)
          }

        }
        // if (PlayerOne.length < WinOptions[key]) {
        //   return false;
        // }
        // if (PlayerTwo.length < WinOptions[key]) {
        //   return false;
        // }
        // 
        // if (PlayerOne === WinOptions[key].sort().join()) {
        //   console.log("PlayerOne has Won!");
        // }
        // if (PlayerTwo === WinOptions[key].sort().join()) {
        //   console.log("PlayerTwo has Won!");
        // } (tried this method but it turned out to be the wrong logic.)
    }
  },


};
TicTacToe.WinTicTacToe();
6个回答

这是解决方案:

使用 ES7ECMAScript 2016

const result = PlayerTwo.every(val => PlayerOne.includes(val));

片段

const PlayerOne = ['B', 'C', 'A', 'D'];
const PlayerTwo = ['D', 'C'];

const result = PlayerTwo.every(val => PlayerOne.includes(val));

console.log(result);

使用 ES5 ( ECMAScript 2009 )

var result = PlayerTwo.every(function(val) {

  return PlayerOne.indexOf(val) >= 0;

});

片段

var PlayerOne = ['B', 'C', 'A', 'D'];
var PlayerTwo = ['D', 'C'];

var result = PlayerTwo.every(function(val) {

  return PlayerOne.indexOf(val) >= 0;

});

console.log(result);


这是在下面的评论中回答问题:

我们如何处理重复?

解:在上面的解中加上检查数组中足够元素个数的准确条件就足够了:

const result = PlayerTwo.every(val => PlayerOne.includes(val) 
    && PlayerTwo.filter(el => el === val).length
       <=
       PlayerOne.filter(el => el === val).length
);

第一种情况的片段

const PlayerOne = ['B', 'C', 'A', 'D'];
const PlayerTwo = ['D', 'C'];

const result = PlayerTwo.every(val => PlayerOne.includes(val) 
    && PlayerTwo.filter(el => el === val).length
       <=
       PlayerOne.filter(el => el === val).length
);

console.log(result);

第二种情况的片段

const PlayerOne = ['B', 'C', 'A', 'D'];
const PlayerTwo = ['D', 'C', 'C'];

const result = PlayerTwo.every(val => PlayerOne.includes(val) 
    && PlayerTwo.filter(el => el === val).length
       <=
       PlayerOne.filter(el => el === val).length
);

console.log(result);

我们如何处理重复?例如: var PlayerOne = ['B', 'C', 'A', 'D']; var PlayerTwo = ['D', 'C']; 上面的比较结果应该是true var PlayerOne = ['B', 'C', 'A', 'D']; var PlayerTwo = ['D', 'C', 'C']; 上面的比较应该是假的
2021-03-17 02:52:11
我只是用你的好问题@Nrupesh 的答案扩展了答案。享受!:)
2021-03-28 02:52:11

如果您使用的是 ES6:

!PlayerTwo.some(val => PlayerOne.indexOf(val) === -1);

如果您必须使用 ES5,请为Mozilla 文档中some函数使用 polyfill ,然后使用常规函数语法:

!PlayerTwo.some(function(val) { return PlayerOne.indexOf(val) === -1 });
更好的是: !PlayerTwo.some(val => !PlayerOne.includes(val));
2021-03-22 02:52:11
使用every比使用倒置的 some 更好,every如果元素为 false 会短路,因此需要相同的时间,并且可读性更高。
2021-04-08 02:52:11

您可以使用这段简单的代码。

PlayerOne.every(function(val) { return PlayerTwo.indexOf(val) >= 0; })
function isSubsetOf(set, subset) {
    return Array.from(new Set([...set, ...subset])).length === set.length;
}
Array.from(...)没有必要。.size直接使用新集:new Set([...set, ...subset]).size
2021-03-20 02:52:11

如果 PlayerTwo 是 PlayerOne 的子集,则 set(PlayerOne + PlayerTwo) 的长度必须等于 set(PlayerOne) 的长度。

var PlayerOne = ['B', 'C', 'A', 'D'];
var PlayerTwo = ['D', 'C'];

// Length of set(PlayerOne + PlayerTwo) == Length of set(PlayerTwo)

Array.from(new Set(PlayerOne) ).length == Array.from(new Set(PlayerOne.concat(PlayerTwo)) ).length
重复怎么办?['B', 'B']的子集['B', 'C', 'A', 'D']吗?还是只是为了['B', 'B', 'C', 'A', 'D']上述逻辑没有处理这些情况。
2021-04-06 02:52:11