如何检查一个数组是不是是 JavaScript 中另一个数组的子集?
Posted
技术标签:
【中文标题】如何检查一个数组是不是是 JavaScript 中另一个数组的子集?【英文标题】:How to check if an array is a subset of another array in JavaScript?如何检查一个数组是否是 JavaScript 中另一个数组的子集? 【发布时间】:2016-12-13 04:17:35 【问题描述】:假设我有两个数组,
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();
【问题讨论】:
Determining whether one array contains the contents of another array in JavaScript/CoffeeScript的可能重复 重复***.com/questions/8628059/… 我会离开数组而只使用字符串,检查子字符串比遍历数组查找匹配集更容易。var test = playerTwo.every(e => playerOne.includes(e));
@yangmei e
是传递给回调(作为每个方法的参数提供的箭头函数)的参数,它与箭头函数的定义一起定义。你可以写和var test = playerTwo.every(function(e) return playerOne.includes(e) );
一样的代码
【参考方案1】:
解决方法如下:
使用 ES7(ECMAScript 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'];上面的比较结果应该是 false 我刚刚用你的好问题@Nrupesh 的答案扩展了答案。享受! :)【参考方案2】:如果你使用的是 ES6:
!PlayerTwo.some(val => PlayerOne.indexOf(val) === -1);
如果您必须使用 ES5,请为 some
函数和 Mozilla documentation 使用 polyfill,然后使用常规函数语法:
!PlayerTwo.some(function(val) return PlayerOne.indexOf(val) === -1 );
【讨论】:
更好:!PlayerTwo.some(val => !PlayerOne.includes(val));
使用every比倒置一些更好,every
如果元素为假会短路,所以需要同样的时间,而且可读性要好很多。【参考方案3】:
您可以使用这段简单的代码。
PlayerOne.every(function(val) return PlayerTwo.indexOf(val) >= 0; )
【讨论】:
【参考方案4】:function isSubsetOf(set, subset)
return Array.from(new Set([...set, ...subset])).length === set.length;
【讨论】:
Array.from(...)
不是必需的。直接使用新集合的.size
:new Set([...set, ...subset]).size
【参考方案5】:
如果 PlayerTwo 是 PlayerOne 的子集,则集合的长度(PlayerOne + PlayerTwo)必须等于集合的长度(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']
?上述逻辑并没有处理这些情况。【参考方案6】:
如果你想比较两个数组并考虑顺序,这里有一个解决方案:
let arr1 = [ 'A', 'B', 'C', 'D' ];
let arr2 = [ 'B', 'C' ];
arr1.join().includes(arr2.join()); //true
arr2 = [ 'C', 'B' ];
arr1.join().includes(arr2.join()); //false
【讨论】:
【参考方案7】:这对我来说似乎最清楚:
function isSubsetOf(set, subset)
for (let i = 0; i < set.length; i++)
if (subset.indexOf(set[i]) == -1)
return false;
return true;
它还有一个好处,就是一发现非会员就爆发。
【讨论】:
.every 和 .some 都会在最终结果公布后提前退出。【参考方案8】:这是一个利用 set 数据类型及其has
函数的解决方案。
let PlayerOne = ['B', 'C', 'A', 'D', ],
PlayerTwo = ['D', 'C', ],
[one, two] = [PlayerOne, PlayerTwo, ]
.map( e => new Set(e) ),
matches = Array.from(two)
.filter( e => one.has(e) ),
isOrisNot = matches.length ? '' : ' not',
message = `$PlayerTwo is$isOrisNot a subset of $PlayerOne`;
console.log(message)
Out: D,C is a subset of B,C,A,D
【讨论】:
以上是关于如何检查一个数组是不是是 JavaScript 中另一个数组的子集?的主要内容,如果未能解决你的问题,请参考以下文章
如何检查 Javascript 中是不是存在字符串/数组/对象? [复制]