2017-02-09 239 views
0

檢查第一2個元件的元件的:比較兩個陣列,其是在另一個陣列

checking first 2 element

檢查接下來的2個元素:

checking next 2 element

我有這樣的陣列中,需要比較各2個元件

var my_arr = [ 
      [0,1,2], 
      [0,2,1], 
      [1,0,2], 
      [1,2,0], 
      [2,0,1], 
      [2,1,0] 
     ]; 

我需要得到這個作爲最終結果。腳本必須比較每個數組的每2個元素,並得到唯一的一個人

var new_arr = [ 
      [0,1,2], 
      [0,2,1], 
     ]; 
+1

請仔細閱讀[問]。重要短語:「搜索和研究」和「解釋......阻止你自己解決它的任何困難」。 –

+1

結果的邏輯是什麼?你想從比較兩個元素得到什麼​​信息?您實際上沒有解釋問題或提供任何可以幫助我們的信息。請閱讀[問]。 –

+0

有圖片看着它,你可以理解的邏輯 – David

回答

0

你可以使用一個嵌套的方式與迭代與ij圖形部分和部分測試與kl。如果找到兩個元素,則陣列拼接。

var array = [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]], 
 
    i = 0, j, k, l; 
 

 
while (i < array.length) { 
 
    j = 0; 
 
    while (j + 1 < array[i].length) { 
 
     k = i + 1; 
 
     test: while (k < array.length) { 
 
      l = 0; 
 
      while (l + 1 < array[k].length) { 
 
       if (array[i][j] === array[k][l] && array[i][j + 1] === array[k][l + 1]) { 
 
        array.splice(k, 1); 
 
        continue test; 
 
       } 
 
       l++; 
 
      } 
 
      k++; 
 
     } 
 
     j++; 
 
    } 
 
    i++; 
 
} 
 

 
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+1

非常感謝! – David