繁体   English   中英

比较两个数组中的对象

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM