2011-09-29 100 views
1

我想比较2个“多维”数组(数组嵌套在数组中)。在JavaScript中比较多维数组

var old_dataArray=new Array(("id-2", "message", "user"), ("id-1", "message", "user"), ("id-0", "message", "user")); 
var new_dataArray=new Array(("id-3", "message", "user"), ("id-2", "message", "user"), ("id-1", "message", "user")); 
在这种情况下

,我想获得的阵列(“ID-3”,“消息”,“用户”),其仅包含在“old_dataArray”,而不是在“new_dataArray”。

我试着用这里介绍的array_diff函数。 http://phpjs.org/functions/array_diff:309 但它并不真正起作用!

+3

您没有嵌套数组,因为您打算成为内部数组的方括号而不是方括号。说到这一点,不要理会'new Array(1,2,3)'语法,只需使用数组文字'[1,2,3]'语法。当你说你想查找只包含在'old_dataArray'中而不是'new_dataArray'中的嵌套数组时,你的意思是按照身份或值来比较每个嵌套数组吗? (也就是说,根据它们是引用相同的数组对象还是找到它们,如果它们具有相同值的元素数量?) – nnnnnn

+0

@nnnnnn:我想你不想让upvotes在适当的位置回答这个问题,呃? – hugomg

+1

@missingno - 哦,upvotes不是一切。我没有发布答案,因为(根据我的评论的后半部分),我觉得问题不清楚如何比较嵌套数组。我想,一旦澄清了这一点,如果没有其他人,我可以发表一个答案,但同时至少我可以指出方括号。 – nnnnnn

回答

3

从我的理解,你想获得new_dataArray中不在old_dataArray中的所有数组,并且我假设如果每个元素中的第一个元素('id-n'元素)是相同的,那么数组的其余部分。你可以这样做是这样的:

// create an array to store our results: 
var results = new Array(); 

// loop through new_dataArray: 
outerloop: 
for (var i = 0; i < new_dataArray.length; ++i) { 

    // loop through old_dataArray to compare the i'th element 
    // in new_dataArray with each in old_dataArray: 
    for (var j = 0; j < old_dataArray.length; ++j) { 

     // check if the ids are the same 
     if (new_dataArray[i][0] == old_dataArray[j][0]) 
      // yes it's there, so move on to the next element of new_dataArray 
      continue outerloop; 

    } 

    // if we get here, continue outerloop; was never called so 
    // this element is not in old_dataArray 
    results.push(new_dataArray[i]); 

} 

// now results contains all arrays that are in new_dataArray 
// but not in old_dataArray 

编辑:但是,如果你希望每个阵列中的所有元素相等,而不只是第(ID-n)的元素,使用此:

// create an array to store our results: 
var results = new Array(); 

// loop through new_dataArray: 
outerloop: 
for (var i = 0; i < new_dataArray.length; ++i) { 

    // loop through old_dataArray to compare the i'th element 
    // in new_dataArray with each in old_dataArray: 
    innerloop: 
    for (var j = 0; j < old_dataArray.length; ++j) { 

     // check if the arrays are the same size: 
     if (new_dataArray[i].length != old_dataArray[j].length) 
      // no, so they must be different 
      continue innerloop; 

     // check if the arrays have the same values 
     for (var k = 0; k < old_dataArray[j].length; ++k) { 

      if (new_dataArray[i][k] != old_dataArray[j][k]) 
       // the k'th element is different 
       continue innerloop; 
     } 

     // if we get here, then we have found a match, so move on 
     continue outerloop; 

    } 

    // if we get here, continue outerloop; was never called so 
    // this element is not in old_dataArray 
    results.push(new_dataArray[i]); 

} 

// now results contains all arrays that are in new_dataArray 
// but not in old_dataArray 
+0

+1为'继续outerloop'。 (我知道有些人不喜欢标签并继续使用,但后来我知道一些人很闷,他们甚至不会跳出非嵌套循环;我喜欢'continue outerloop'语法,特别是有意义的评论你的。) – nnnnnn

+0

谢谢。我总是尽量减少使用临时变量和不必要的条件检查。每个时钟周期都很重要! – megaflop

0
var old_dataArray = [["id-2", "message", "user"], ["id-1", "message", "user"], ["id-0", "message", "user"]]; 
var new_dataArray = [["id-3", "message", "user"], ["id-2", "message", "user"], ["id-1", "message", "user"]]; 

var result = []; 

for (var i = 0; i < new_dataArray.length ; i++) { 
    var isInOldArray = false; 
    console.log(new_dataArray[i][0]); 

    for (var j = 0; j < old_dataArray.length; j++) { 
     if (old_dataArray[j][0] === new_dataArray[i][0]) { 
      isInOldArray = true; 
      break; 
     }; 
    }; 

    if (!isInOldArray) { 
     result.push(new_dataArray[i]); 
    }; 
}; 

alert(result); 
+0

为了提高效率,在设置isInOldArray = true之后,您应该'break';' - 在找到匹配之后继续循环没有意义。 – megaflop

+0

感谢您纠正此问题! – alexl

+1

没问题。实际上,通过在找到匹配时继续外循环,从而消除设置和稍后检查布尔值的需要,可以提高效率。然后我们得到......我的解决方案! :-) – megaflop