JavaScript - 比较两个多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16437307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:40:36  来源:igfitidea点击:

JavaScript - Compare two multidimensional arrays

javascriptfor-loopmultidimensional-arraymatch

提问by mihajlo

I have two multidimensional arrays:

我有两个多维数组:

first is something like (['one','one','three'],['four','five',five'],['one','one','one'])

首先是类似的东西 (['one','one','three'],['four','five',five'],['one','one','one'])

and the second one is like this (['one','one','nine'],['one','one','one'],['two','two'],['two','two','two']...)

第二个是这样的 (['one','one','nine'],['one','one','one'],['two','two'],['two','two','two']...)

Now, what I want is to find match first index of first array with second array, but position of at least first two indexes from boths array must match also, eg.:

现在,我想要的是找到第一个数组的第一个索引与第二个数组的匹配,但是两个数组中至少前两个索引的位置也必须匹配,例如:

first_array (['one','one','three'],['four','five',five'],['one','one','one'])

first_array ( ['one','one','three'],['four','five',five'],['one','one','one'])

will match

会匹配

second_array (['one','one','nine'],['one','one','one'],['two','two']['two','two','two']...)

second_array ( ['one','one','nine'],['one','one','one'],['two','two']['two','two','two ']...)

and output would be eg. 'alert('Match.').

和输出将是例如。'警报('匹配。')。

I have tried

我努力了

for(i=0; i<1; i++){
    if(first_array[0] == second_array) console.log('Match');
    else console.log('No match');
}

but I constantly get 'No match' although there is a match. P.S. in 'for' loop, my i is i<1 because I want to compare only first index of first_array with complete second_array.

但我经常得到“不匹配”,尽管有匹配。PS在'for'循环中,我的i是i<1,因为我只想将first_array的第一个索引与完整的second_array进行比较。

Thanks in advance

提前致谢

回答by Kevin Bowersox

var md1 = [['one','one','three'],['four','five','five'],['one','one','one']];

var md2 = [['one','one','nine'],['one','one','one'],['two','two'],['two','two','two']];

//Iterate through all elements in first array
for(var x = 0; x < md1.length; x++){

    //Iterate through all elements in second array    
    for(var y = 0; y < md2.length; y++){

      /*This causes us to compare all elements 
         in first array to each element in second array
        Since md1[x] stays fixed while md2[y] iterates through second array.
         We compare the first two indexes of each array in conditional
      */
      if(md1[x][0] == md2[y][0] && md1[x][1] == md2[y][1]){
        alert("match found");
        alert("Array 1 element with index " + x + " matches Array 2 element with index " + y);
      }
    }
}

Working Examplehttp://jsfiddle.net/2nxBb/1/

工作示例http://jsfiddle.net/2nxBb/1/

回答by Robbert

Possible duplicate of How to compare arrays in JavaScript?.

可能的重复如何在JavaScript中比较数组?.

For a strict array comparison, check their length and values like so:

对于严格的数组比较,请检查它们的长度和值,如下所示:

var a1 = [1, 2, 3];
var a2 = [1, 2, 3];

array_compare(a1, a2);

function array_compare(a1, a2) {
 if(a1.length != a2.length) {
  return false;
 }
 for(var i in a1) {
  // Don't forget to check for arrays in our arrays.
  if(a1[i] instanceof Array && a2[i] instanceof Array) {
   if(!array_compare(a1[i], a2[i])) {
    return false;
   }
  }
  else if(a1[i] != a2[i]) {
   return false;
  }
 }
 return true;
}