2016-06-15 22 views
-1

我想知道,什麼是一個辦法比較兩個數組,最有效的方法:發現陣列之間的差異,並更新

  1. 發現從數組A缺少的對象相比,B陣列,並增加了他們反對A;
  2. 如果從一個目的是從對象B不同,它更新

任何想法的屬性(由屬性「ID」識別的對象)?

+0

我是一個表示哈希表中一個數組的所有元素的巨大粉絲,並且通過第二次迭代通過另一個數組來做想要的部分。 –

+0

你不可能有一個'最有效'的方式。此外,你基本上只是將數組B複製到數組A中,所以最好這樣說。 – Tobsta

+0

你目前使用什麼方法,你有什麼問題? – Lix

回答

0

通常情況下,最有效的方法是本地方法,所以這樣的事情應該執行得很好:

console.clear(); 
 

 
var one = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; 
 
var two = ['a', 'b', 'c', 'e', 'g']; 
 

 
function insertMissing(insertTo, insertFrom) { 
 
\t insertFrom.filter(function (from) { 
 
\t \t return insertTo.some(function (too) { 
 
\t \t \t return too === from 
 
\t \t }) == false; 
 
\t }).forEach(function (el) { 
 
\t \t insertTo.push(el); 
 
\t }) 
 
} 
 
insertMissing(two, one); 
 

 
console.log(one, two);

我需要知道確切的對象標記知道該怎麼匹配,但這表明了基礎知識。

下面是一個片段與對象展示它:

console.clear(); 
 

 
var one = [{ 
 
\t \t 'id' : 'a' 
 
\t }, { 
 
\t \t 'id' : 'b' 
 
\t }, { 
 
\t \t 'id' : 'c' 
 
\t }, { 
 
\t \t 'id' : 'd' 
 
\t }, { 
 
\t \t 'id' : 'e' 
 
\t }, { 
 
\t \t 'id' : 'f' 
 
\t }, { 
 
\t \t 'id' : 'g' 
 
\t } 
 
]; 
 
var two = [{ 
 
\t \t 'id' : 'a' 
 
\t }, { 
 
\t \t 'id' : 'b' 
 
\t }, { 
 
\t \t 'id' : 'c' 
 
\t }, { 
 
\t \t 'id' : 'e' 
 
\t }, { 
 
\t \t 'id' : 'g' 
 
\t } 
 
]; 
 

 
function insertMissing(insertTo, insertFrom) { 
 
\t insertFrom.filter(function (from) { 
 
\t \t return insertTo.some(function (too) { 
 
\t \t \t return too.id === from.id 
 
\t \t }) == false; 
 
\t }).forEach(function (el) { 
 
\t \t insertTo.push(el); 
 
\t }) 
 
} 
 
insertMissing(two, one); 
 

 
console.log(one); 
 
console.log(two);

最後,這將覆蓋對象的屬性版本:

console.clear(); 
 

 
var one=[{'id':'a','value':1},{'id':'b','value':1},{'id':'c','value':1},{'id':'d','value':1},{'id':'e','value':3},{'id':'f','value':1},{'id':'g','value':1}]; 
 
var two=[{'id':'a','value':1},{'id':'b','value':1},{'id':'c','value':1},{'id':'e','value':1},{'id':'g','value':1}]; 
 

 
function insertMissing(insertTo, insertFrom) { 
 
\t //Validate existing elements 
 
\t insertTo.forEach(function (too) { 
 
\t \t var from; 
 
\t \t insertFrom.forEach(function (f, index) { 
 
\t \t \t if (f.id == too.id && typeof from == 'undefined') { 
 
\t \t \t \t from = index; 
 
\t \t \t } 
 
\t \t }); 
 
\t \t if (typeof from == "number") { 
 
\t \t \t for (var i in insertFrom[from]) { 
 
\t \t \t \t if (insertFrom[from].hasOwnProperty(i)) { 
 
\t \t \t \t \t if (insertFrom[from][i] !== too[i]) { 
 
\t \t \t \t \t \t too[i] = insertFrom[from][i]; 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t } 
 
\t }); 
 
\t //Insert missing elements 
 
\t insertFrom.filter(function (from) { 
 
\t \t return insertTo.some(function (too) { 
 
\t \t \t return too.id === from.id 
 
\t \t }) == false; 
 
\t }).forEach(function (el) { 
 
\t \t insertTo.push(el); 
 
\t }) 
 
} 
 
insertMissing(two, one); 
 

 
console.log(one); 
 
console.log(two);

+0

請記住,OP已要求數組由對象填充,這將由'id'屬性標識。 – Tobsta

+0

@Tobsta我知道。這就是爲什麼我提出了關於標記的評論。我編輯了我的答案,以顯示它如何使用'id'參數。 –

+0

對不起,我沒有看到,但你真正需要知道或猜測的是對象遵循一個基本的'{id:x,... other properties ...}'格式。 – Tobsta

相關問題