2011-09-29 105 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