JavaScript - 比较两个多维数组

4

我有两个多维数组:

第一个数组类似于(['one','one','three'],['four','five',five'],['one','one','one'])

第二个数组类似于(['one','one','nine'],['one','one','one'],['two','two'],['two','two','two']...)

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

第一个数组 (['one','one','three'],['four','five',five'],['one','one','one'])

将与

第二个数组 (['one','one','nine'],['one','one','one'],['two','two']['two','two','two']...)

匹配,并且输出将会是'alert('Match.')

我尝试过

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

尽管有匹配项,但我不断地收到“无匹配项”的消息。 附注:在“for”循环中,我的i为i<1,因为我只想将第一个数组的第一个索引与完整的第二个数组进行比较。
提前感谢。
3个回答

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

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


我会尝试翻译,5分钟后回来。 - mihajlo
@mihajloWR,我刚刚注意到一个或两个错误,请使用更新版本。主要是当我应该使用md1[x]和md2[y]时,我一直在使用md1[y]。 - Kevin Bowersox
似乎正在运作...需要根据我的需求进行调整,但总体上是正常的。 你有空闲时间解释一下逻辑吗?这样我就能完全理解它了吗? - mihajlo
@mihajloWR 我添加了注释以帮助解释正在发生的事情,基本上它正在迭代第二个数组中的每个元素,对于第一个数组中的每个元素进行比较。 - Kevin Bowersox
第一个版本按照我的要求工作,但是新的更新不行,只是让你知道一下 :). 感谢您的解释和时间,您帮了我很多。 - mihajlo
请注意,该解决方案仅适用于您确定数组将包含字符串或数字的情况。对于可能包含数组的数组,请参见我的解决方案。 - Robbert

3

可能是JavaScript如何比较两个数组?的重复问题。

要严格比较两个数组,可以检查它们的长度和值,如下所示:

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

1

2种方法,如果这个对您足够简单的话

JSON.stringify(tree1) === JSON.stringify(tree2)

如果没有,使用这个:递归处理多维数组和对象。
treesAreSame(tree1, tree2) {
        if (tree1 === tree2) {
            return true;
        }
        if (tree1 === null || tree1 === undefined || tree2 == null) {
            return false;
        }
        if (Array.isArray(tree1) !== Array.isArray(tree2)) {
            return false;
        }
        if (tree1.length !== tree2.length) {
            return false;
        }
        if (isArray(tree1)) {
            for (let i = 0; i < tree1.length; ++i) {
                let t1 = tree1[i];
                let t2 = tree2[i];
                if (isArray(t1) || isObject(t1)) {
                    if (!treesAreSame(t1, t2)) {
                        return false;
                    }
                } else {
                    if (t1 !== t2) {
                        return false;
                    }
                }
            }
        }
        if (isObject(tree1)) {
            for (const k of Object.keys(tree1)) {
                let t1 = tree1[k];
                let t2 = tree2[k];
                if (isArray(t1) || isObject(t1)) {
                    if (!treesAreSame(t1, t2)) {
                        return false;
                    }
                } else {
                    if (t1 !== t2) {
                        return false;
                    }
                }
            }
        }
        return true;
    };
    isObject(a) {
        return (!!a) && (a.constructor === Object);
    };
    isArray(a) {
        return (!!a) && (a.constructor === Array);
    };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接