在javascript中将2d数组与1d数组进行比较

时间:2016-06-17 04:58:02

标签: javascript arrays multidimensional-array

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我想检查w [0]所有值是否存在于p中,依此类推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;
                            }   
                        }

                    }

2 个答案:

答案 0 :(得分:1)

我只是通过Array.prototype.intersect()的简单发明来做到这一点,其余的就是这么简单的单行。

&#13;
&#13;
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);
&#13;
&#13;
&#13;

好的,我们有一个通用的Array方法,可以找到两个数组的交集。 (在它被调用的数组和作为参数提供的数组之间)它基本上是一个过滤器,检查第一个数组的每个项目以查看它是否包含在第二个数组中。所以我们过滤掉两个数组中存在的项目。

要获得已过滤的数组,我们再次使用Array.prototype.filter()。这次我们的第一个数组是winmoves,其中包括我们将检查每个与player1数组的交集的数组。如果交叉点长度等于winmove项目的长度,则意味着winmove项目的所有元素都存在于player1数组中。所以我们将该数组项返回到filtered

如果不使用交叉方法特定于您的情况,您可以使用Array.prototype.every(),如下所示;

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以做的是使用嵌套for循环

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中工作,因为这只是基本的伪代码。 Flag只是一个变量,如果找到或未找到该值,则为每个新值重置。为了提高效率,您可以在设置flag = 1后进行中断/返回,但这是