JavaScript - 比较两个多维数组 [英] JavaScript - Compare two multidimensional arrays

查看:197
本文介绍了JavaScript - 比较两个多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个多维数组:

第一个是类似于code>(['one','one','three'],[ '四','五',五'',['一','一','一'])



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



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

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

$ p
$ b $ second_array( ['one','one','nine'] ,['one', 'two','two','two','two','two'] ...)

和输出将是例如。 'b
$ b

我已经尝试了

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

但是我经常得到'不匹配',虽然有匹配。
P.S.在'for'循环中,我的i是< 1,因为我只想比较first_array的第一个索引和完整的second_array。



预先感谢

解决方案

  var md1 = [['one','one','three'],[' , '五', '5'],[ '一', '一', '一']]; 

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

//遍历第一个数组中的所有元素
for(var x = 0; x< md1.length; x ++){

//迭代第二个数组中的所有元素
for(var y = 0; y< md2.length; y ++){

/ *这使得我们比较第一个数组中的所有元素
到第二个数组中的每个元素
由于md1 [x]保持固定,而md2 [y]遍历第二个数组。
我们比较条件
* /
中每个数组的前两个索引if(md1 [x] [0] == md2 [y] [0]& md1 [x ] [1] == md2 [y] [1]){
alert(match found);
alert(索引为+ x +的数组1元素与索引为+ y的数组2元素匹配;





工作示例


$ / / strong> http://jsfiddle.net/2nxBb/1/


I have two multidimensional arrays:

first is something like (['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']...)

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'])

will match

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.

Thanks in advance

解决方案

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 Example http://jsfiddle.net/2nxBb/1/

这篇关于JavaScript - 比较两个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆