[英]compare 2d array with 1d array in javascript
var player1=["t1", "t9", "t7", "t8", "t2"];
var player2=["t5", "t3", "t4", "t6"];
var winmoves=[[t1,t2,t3],[t4,t5,t6],[t7,t8,t9],[t1,t4,t7],[t2,t5,t8],[t3,t6,t9],[t1,t5,t9],[t3,t5,t7]];
else if (moves>=3 && moves<=9) {
moves+=1;
if (turn==1) {
var i=0;
turn=2;
x.innerHTML="<img src='x.png'/>";
player1.push(x.id);
while(i<=moves){
if (winmoves[i] in player1) {
alert("player1 wins");
document.getElementById("player1").innerHTML=1;
}
i+=1;
}
}
我在javascript中有1d數組player1和2d數組winmoves,我想檢查p中是否存在w [0]的所有值,依此類推,對於w [1],w [2]等。 如果(winmoves[i] in player1)
條件不起作用。 我不知道我是否在寫這篇文章。 幫助我,我被困在這里,我該怎么做。
即使進行了這些更改,它也不起作用。
else if (moves>=3 && moves<9) {
moves+=1;
if (turn==1) {
var i=0;
turn=2;
x.innerHTML="<img src='x.png'/>";
player1.push(x.id);
while(i<=moves){
mapped1 = winmoves.map(a1 => a1.every(e1 => player1.includes(e1)));
if (mapped1[i]) {
alert("player1 wins");
document.getElementById("player1").innerHTML=1;
}
i+=1;
}
}
else if (turn==2) {
turn=1;
var i=0;
x.innerHTML="<img src='o.png'/>";
turn=1;
player2.push(x.id);
while(i<=moves)
{
mapped2 = winmoves.map(a => a.every(e => player2.includes(e)));
if (mapped2[i]) {
alert("player2 wins");
document.getElementById("player2").innerHTML=1;
}
i+=1;
}
}
}
我將使用Array.prototype.intersect()
的簡單發明來簡單地做到這一點,其余就是這么簡單的單個內襯。
Array.prototype.intersect = function(a) { return this.filter(e => a.includes(e)); }; var player1 = ["t1","t2","t3","t5","t7"], winmoves = [["t1","t2","t3"],["t4","t5","t6"],["t7","t8","t9"],["t1","t4","t7"],["t2","t5","t8"],["t3","t6","t9"],["t1","t5","t9"],["t3","t5","t7"]]; filtered = winmoves.filter(a => a.intersect(player1).length == a.length); mapped = winmoves.map(a => a.intersect(player1).length == a.length); console.log(filtered); console.log(mapped);
好了,我們有一個通用的Array方法,可以找到兩個數組的交集。 (在被調用的數組和作為參數提供的數組之間)基本上是一個過濾器,用於檢查第一個數組的每個項目,以查看它是否包含在第二個數組中。 因此,我們過濾掉兩個數組中都存在的項。
為了獲得過濾后的數組,我們再次利用Array.prototype.filter()
。 這次,我們的第一個數組是winmoves
,其中包括數組,我們將檢查每個與player1
數組的交集。 如果交集長度等於winmove
的項的長度,這意味着的所有元素winmove
的項中現有player1
陣列。 因此,我們將該數組項返回給filtered
。
特定於您的情況,而無需使用相交方法,可以使用Array.prototype.every()
,如下所示;
var player1 = ["t1","t2","t3","t5","t7"], winmoves = [["t1","t2","t3"],["t4","t5","t6"],["t7","t8","t9"],["t1","t4","t7"],["t2","t5","t8"],["t3","t6","t9"],["t1","t5","t9"],["t3","t5","t7"]]; filtered = winmoves.filter(a => a.every(e => player1.includes(e))); mapped = winmoves.map(a => a.every(e => player1.includes(e))); console.log(filtered); console.log(mapped);
你能做的就是使用嵌套循環
for(var j = 0, j < elemInP, j++){
int flag = 0;
for(var x = 0, x < elemInWx, x++){
for(var y = 0, y < elemInWy, y++){
if(p[j] == w[x][y]){
flag = 1;
/*There is no need to run this loop once the value has been found*/
break;
}
}
if(flag){
/*If we have found the value no need to keep looking*/
break;
}
}
if(!flag){
print p[j] is not in w;
}
}
這只是比較兩個數組的一種方法的一般想法。 該代碼的實際語法需要進行編輯才能在JavaScript中工作,因為這只是基本的偽代碼。 標志只是一個變量,它保存是否找到該值,並為每個新值重置。 為了提高效率,您可以在設置標志= 1之后進行一次中斷/返回,但這是
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.