[英]Comparing objects in two arrays
我想比較兩個數組中的對象,如果它們不相同,則將它們添加到數組中。
第一個數組
[
{
"email": "a@a.com"
},
{
"email": "b@b.com"
},
{
"email": "c@c.com"
},
{
"email": "d@d.com"
}
]
第二陣列
[
{
"email": "v@v.com"
},
{
"email": "k@k.com"
},
{
"email": "g@g.com"
}
]
檢查功能
if($scope.participants.length > 0){
result.forEach(function (resultElement) {
if(!$scope.participants.includes(resultElement) ) {
$scope.participants.push(resultElement);
}
})
result = [];
console.log($scope.participants);
}
我檢查了調試,它在 if 條件下下降。
您需要了解兩個對象不相等和相同。
例如 {} === {} 返回 false
如果要比較對象,則需要比較每個對象的每個原始元素。
原語包括數字、字符串、布爾值等,而不是對象或數組(它們也是對象)。
b1 = [ { id: 0, email: 'john@' }, { id: 1, email: 'mary@' }, { id: 2, email: 'pablo@' }, { id: 3, email: 'escobar@' } ]; b2 = [ { id: 0, email: 'john@' }, { id: 1, email: 'mary@' } ]; var res = this.b1.filter(item1 => !this.b2.some(item2 => (item2.id === item1.id && item2.name === item1.name))) console.log("check more is working",res);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.