假设我有两个数组,
var PlayerOne = ['B', 'C', 'A', 'D'];
var PlayerTwo = ['D', 'C'];
使用javascript检查arrayTwo是否是arrayOne的子集的最佳方法是什么?
原因:我试图理清游戏Tic tac toe的基本逻辑,并陷入中间。无论如何,这是我的代码......谢谢堆!
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();
答案 0 :(得分:36)
正确的解决方案是:
在 ES6 语法中:
PlayerTwo.every(val => PlayerOne.includes(val));
或 ES5 语法:
PlayerTwo.every(function(val) { return PlayerOne.indexOf(val) >= 0; });
答案 1 :(得分:15)
如果您使用的是ES6:
!PlayerTwo.some(val => PlayerOne.indexOf(val) === -1);
如果必须使用ES5,请对some
函数Mozilla documentation使用polyfill,然后使用常规函数语法:
!PlayerTwo.some(function(val) { return PlayerOne.indexOf(val) === -1 });
答案 2 :(得分:7)
您可以使用这段简单的代码。
PlayerOne.every(function(val) { return PlayerTwo.indexOf(val) >= 0; })
答案 3 :(得分:2)
如果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
答案 4 :(得分:0)
这对我来说最清楚:
function isSubsetOf(set, subset) {
for (let i = 0; i < set.length; i++) {
if (subset.indexOf(set[i]) == -1) {
return false;
}
}
return true;
}
一旦找到非成员,它还具有爆发的优势。
答案 5 :(得分:0)
这是一个利用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
答案 6 :(得分:0)
Bitmap bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.changing);
TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if(!textRecognizer.isOperational()){
Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT).show();
}
else
{
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> item = textRecognizer.detect(frame); //Yeh Frame Detect Kr rha he
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i<item.size();i++){
TextBlock myitem = item.valueAt(i);
sb.append(myitem.getValue());
sb.append("\n");
}
答案 7 :(得分:0)
如果您要比较两个数组并同时考虑顺序问题,这是一个解决方案:
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