简体   繁体   English

比较 Javascript 中的两个 Arrays 以找到相同的相似之处和其他相似之处

[英]Comparing Two Arrays in Javascript to Find Identical Similarities and Other Similarities

I have two arrays in Javascript: code and submittedCode.我在Javascript中有两个arrays:code和submittedCode。 I am trying to compare the two arrays. Each of the arrays is 4 integers long, and each integer is a random value of 1 to 6. I also have two variables: red and white.我正在尝试比较两个 arrays。每个 arrays 都是 4 个整数长,每个 integer 是 1 到 6 的随机值。我还有两个变量:红色和白色。 When the two are compared, the red variable should be set to the number of similarities in the arrays that are the same number, same index.两者比较时,红色变量应设置为arrays中相同数、相同索引的相似数。 White should be set to the number of similarities in the array that are the same number, but different indexes. White 应该设置为数组中相同数量但不同索引的相似数量。 For example, if the code array is [1, 3, 6, 5] and submittedCode is [1, 6, 4, 5], then red would be set to 2, and white would be set to 1. This is the same logic as the game Mastermind if anyone has played that.比如code数组是[1, 3, 6, 5],submittedCode是[1, 6, 4, 5],那么red设置为2,white设置为1,这个是一样的如果有人玩过的话,逻辑就像游戏策划者一样。 Below is what I have tried, but it is not working as intended.以下是我尝试过的方法,但它没有按预期工作。

for(let i = 0; i < code.length; i++) {
        if(code[i] == submittedCode[i])
        {
            code.splice(i, 1);
            submittedCode.splice(i, 1);
            red++;
            //console.log(i);
        }
    }
    console.log(code);
    var saveLength = code.length;

    code = code.filter(function(val) {
  return submittedCode.indexOf(val) == -1;
    });

    white = saveLength - code.length;

    console.log(red + ", " + white);
let arr=[1,3,1,2]       //two array we operate on
let arr2=[4,4,1,2]

let red=0
let white=0

//we need to check the current length of remaining array
let currentLength=arr.length            

for(let i=0; i<currentLength; i++){
    if(arr[i]===arr2[i]){       
    arr.splice(i,1)             //if same number same index, remove this item from array
    arr2.splice(i,1)
    currentLength-=1            //currentLength-1 because we reduced the array
    i-=1                                    //i-1 because we'd skip the next element
    red+=1                              //update red
  }
}

//we basically do the same thing but with white
//but in the second array we look for the first index of the current element and remove that
for(let i=0; i<arr.length; i++){
if(arr2.includes(arr[i])){   
    //1: I should've taken away the item from arr2 first
    //2: arr2.splice(arr2.findIndex(f=>f===arr[i],1))  notice I messed up where I put the 1 at the end
    arr2.splice(arr2.findIndex(f=>f===arr[i]),1)
    arr.splice(i,1)  
    i-=1
    white+=1
}
}

Now this might not be the most optimal solution, you can do this in one loop, but for visibility I created 2 for loops, in the first we check the 'red' elements, and we take those out in the second loop we check if there are any 'white' elements, and we take those out.现在这可能不是最佳解决方案,您可以在一个循环中执行此操作,但为了可见性,我创建了 2 个 for 循环,在第一个循环中我们检查“红色”元素,然后在第二个循环中将它们取出来检查是否有任何“白色”元素,我们把它们去掉。

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

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