2015-08-21 57 views
4

如何在兩個不同的數組中找到具有不同值的元素?在javascript中找到兩個不同數組對象中具有不同值的元素

第一個數組

[Object { id_request="009", comment_bapak="First Comment"}, 
Object { id_request="010", comment_bapak="Second Comment"}, 
Object { id_request="012", comment_bapak=null} 
] 

第二個數組

[Object { id_request="009", comment_bapak="First Comment"}, 
Object { id_request="010", comment_bapak="Second Comment"}, 
Object { id_request="012", comment_bapak="New comment here ..... "} 
] 

我會提醒元素和它的價值。示例id_request「003」New comment here .....

+0

你期待什麼輸出?它在哪裏是id_request「003」? –

+0

我會通過js –

回答

1

通過執行嵌套的filter可以找到兩個數組的差異,只返回項b中未包含的項目a的項目。例如:

var first = [{ id_request:"009", comment_bapak:"First Comment"}, 
 
{ id_request:"010", comment_bapak:"Second Comment"}, 
 
{ id_request:"012", comment_bapak:null} 
 
]; 
 

 
var second = [{ id_request:"009", comment_bapak:"First Comment"}, 
 
{ id_request:"010", comment_bapak:"Second Comment"}, 
 
{ id_request:"012", comment_bapak:"New comment here ..... "} 
 
]; 
 

 
// returns an array containing the unique objects 
 
var difference = function(arr1, arr2) { 
 
    return arr2.filter(function(item) { 
 
    // filter all the matches and return the negation (0 matches = true = keep in array) 
 
    return !arr1.filter(function(firstItem) { 
 
     // compare comment_bapak 
 
     return firstItem.comment_bapak == item.comment_bapak; 
 
    }).length; 
 
    }); 
 
}; 
 

 
var differentItems = difference(first, second); 
 

 
alert(differentItems[0].comment_bapak);

2

濾波方法計算策略jsfiddle

var diffItems = function (firstAr, secAr) { 
    return firstAr.filter(function (fArItm) { 
     return secAr.filter(function (sArItm) { 
      return fArItm.comment_bapak === sArItm.comment_bapak; 
     }).length === 0; 
    }); 
}; 

var arr2 = [{ 
    id_request: "009", 
    comment_bapak: "First Comment" 
}, { 
    id_request: "010", 
    comment_bapak: "Second Comment" 
}, { 
    id_request: "012", 
    comment_bapak: null 
}]; 

var arr1 = [{ 
    id_request: "009", 
    comment_bapak: "First Comment" 
}, { 
    id_request: "010", 
    comment_bapak: "Second Comment" 
}, { 
    id_request: "012", 
    comment_bapak: "New comment here ..... " 
}]; 

console.log(diffItems(arr1, arr2)[0]['comment_bapak']); 
2

它總是goog,以減少時間複雜度。

如果id和目標attr是固定的(在你的情況,這是id_requestcomment_bapak),你可以只使用一個對象來第一個列表轉換爲map

然後對於第二個列表中的每個項目,您只需要使用地圖來獲取第一個列表中的相關項目,然後比較它們。

複雜度將變爲O(m + n)而不是O(m * n)

var first = [{ id_request:"009", comment_bapak:"First Comment"}, 
 
{ id_request:"010", comment_bapak:"Second Comment"}, 
 
{ id_request:"012", comment_bapak:null} 
 
]; 
 

 
var second = [{ id_request:"009", comment_bapak:"First Comment"}, 
 
{ id_request:"010", comment_bapak:"Second Comment"}, 
 
{ id_request:"012", comment_bapak:"New comment here ..... "} 
 
]; 
 

 
var difference = function(list1, list2, id, attr) { 
 
    var map = {}; 
 
    
 
    // Create map. 
 
    list1.forEach(function(item) { 
 
    map[item[id]] = item; 
 
    }); 
 
    
 
    // Find diff. 
 
    return list2.filter(function(item) { 
 
    var target = map[item[id]]; 
 
    // Return if the item is not exist in first, or the target attr is different. 
 
    return (typeof target === 'undefined' || item[attr] !== target[attr]); 
 
    }); 
 
} 
 

 
var diffs = difference(first, second, 'id_request', 'comment_bapak'); 
 
console.log(diffs); 
 
console.log(diffs[0].comment_bapak);

+0

創建一個提醒,即「請求003的新評論」好處,這是一個更快的解決方案。 –

相關問題