4

我有两个多维数组:

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

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

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

first_array ( ['一','一','三'] ,['四','五',五'],['一','一','一'])

将匹配

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

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

我努力了

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

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

提前致谢

4

3 回答 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/

于 2013-05-08T09:48:27.270 回答
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;
}
于 2013-05-08T09:49:26.230 回答
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);
    };
于 2021-05-12T09:41:47.450 回答