简体   繁体   English

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

[英]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;
                        }
                    }

i have 1d array player1 and 2d array winmoves in javascript and i want to check that w[0]'s all values are present in p and so on for w[1],w[2],etc. 我在javascript中有1d数组player1和2d数组winmoves,我想检查p中是否存在w [0]的所有值,依此类推,对于w [1],w [2]等。 if condition with (winmoves[i] in player1) is not working. 如果(winmoves[i] in player1)条件不起作用。 i don't know if i am writing this write. 我不知道我是否在写这篇文章。 help me guys i am stuck here how can i do so. 帮助我,我被困在这里,我该怎么做。

It is not working even after i have made these changes. 即使进行了这些更改,它也不起作用。

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;
                            }   
                        }

                    }

I would simply do this with a simple invention of Array.prototype.intersect() and the rest is such a straightforward single liner. 我将使用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); 

OK we have a generic Array method which finds the intersection of two arrays. 好了,我们有一个通用的Array方法,可以找到两个数组的交集。 (inbetween the array it's called upon and the one provided as argument) It's basically a filter checking each item of first array to see whether it is included in the second array. (在被调用的数组和作为参数提供的数组之间)基本上是一个过滤器,用于检查第一个数组的每个项目,以查看它是否包含在第二个数组中。 So we filter out the items exist in both arrays. 因此,我们过滤掉两个数组中都存在的项。

To obtain the filtered array we utilize Array.prototype.filter() again. 为了获得过滤后的数组,我们再次利用Array.prototype.filter() This time our first array is winmoves which includes arrays that we will check for each the intersection with player1 array. 这次,我们的第一个数组是winmoves ,其中包括数组,我们将检查每个与player1数组的交集。 If the intersection length is equal to winmove 's item's length that means all elements of winmove 's item is existing in the player1 array. 如果交集长度等于winmove的项的长度,这意味着的所有元素winmove的项中现有player1阵列。 So we return that array item to the filtered . 因此,我们将该数组项返回给filtered

Specific to your case without using an intersect method you can utilize Array.prototype.every() as follows; 特定于您的情况,而无需使用相交方法,可以使用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); 

What you could do is use nested for loops 你能做的就是使用嵌套循环

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;
    }
}

This is just a general idea of one way to compare the two arrays. 这只是比较两个数组的一种方法的一般想法。 The actual syntax of the code will need to be edited to work in JavaScript as this is just basic pseudocode. 该代码的实际语法需要进行编辑才能在JavaScript中工作,因为这只是基本的伪代码。 Flag is just a variable that holds if the value was found or not and is reset for each new value. 标志只是一个变量,它保存是否找到该值,并为每个新值重置。 For efficiency, you could have a break/return after setting flag = 1, but this is 为了提高效率,您可以在设置标志= 1之后进行一次中断/返回,但这是

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM