比较下划线js中的两个对象数组

Posted

技术标签:

【中文标题】比较下划线js中的两个对象数组【英文标题】:Compare two arrays of objects in underscorejs 【发布时间】:2016-08-02 16:31:21 【问题描述】:

我有两个对象数组

var arr1 =
    [
    
        "lastInteracted": "2016-03-31T11:13:09.000Z",
        "email": "concierge@inbound.com",
        "interactionCount": 2
    ,
    
        "lastInteracted": "2016-03-31T21:06:19.000Z",
        "email": "jbi@salesforce.com",
        "interactionCount": 1
    ,
    
        "lastInteracted": "2016-03-29T11:15:41.000Z",
        "email": "abc@insightsquared.com",
        "interactionCount": 1
    ,
    
        "lastInteracted": "2016-03-24T10:02:29.000Z",
        "email": "diana@hubspot.com",
        "interactionCount": 1
    
    ]

var arr2 =
[
    
        "lastInteracted": "2016-03-31T11:13:09.000Z",
        "email": "concierge@inbound.com",
        "interactionCount": 2
    ,
    
        "lastInteracted": "2016-03-31T21:06:19.000Z",
        "email": "jbi@salesforce.com",
        "interactionCount": 4
    ,
    
        "lastInteracted": "2016-03-29T11:15:41.000Z",
        "email": "kstachowski@insightsquared.com",
        "interactionCount": 1
    ,
    
        "lastInteracted": "2016-03-24T10:02:29.000Z",
        "email": "hammer@hubspot.com",
        "interactionCount": 1
    ,
    
        "lastInteracted": "2016-03-24T10:02:29.000Z",
        "email": "life@hubspot.com",
        "interactionCount": 10
    ,
    
        "lastInteracted": "2016-03-24T10:02:29.000Z",
        "email": "mike@hubspot.com",
        "interactionCount": 18
    
]

我想合并这两个数组,这样如果两个数组中都存在一个对象的电子邮件,则将 arr1 中的 interactionCount 与 arr2 进行比较,否则返回 arr1 或 arr2 的交互计数。

结果将是

var result = [
    
        "lastInteracted": "2016-03-31T11:13:09.000Z",
        "email": "concierge@inbound.com",
        "interactionCount": 0
    ,
    
        "lastInteracted": "2016-03-31T21:06:19.000Z",
        "email": "jbi@salesforce.com",
        "interactionCount": -4
    ,
    
        "lastInteracted": "2016-03-29T11:15:41.000Z",
        "email": "abc@insightsquared.com",
        "interactionCount": 1
    ,
    
        "lastInteracted": "2016-03-24T10:02:29.000Z",
        "email": "diana@hubspot.com",
        "interactionCount": 1
    ,
    
        "lastInteracted": "2016-03-29T11:15:41.000Z",
        "email": "kstachowski@insightsquared.com",
        "interactionCount": 1
    ,
    
        "lastInteracted": "2016-03-24T10:02:29.000Z",
        "email": "hammer@hubspot.com",
        "interactionCount": 1
    ,
    
        "lastInteracted": "2016-03-24T10:02:29.000Z",
        "email": "life@hubspot.com",
        "interactionCount": 10
    ,
    
        "lastInteracted": "2016-03-24T10:02:29.000Z",
        "email": "mike@hubspot.com",
        "interactionCount": 18
    
]

【问题讨论】:

你试过了吗? ***.com/a/13514962/1702612 Merging two collections using Underscore.JS的可能重复 我已经完成了这些解决方案,但它们并没有满足我的需求。如果您可以查看结果数组,也许它会更清晰,因为我不仅想消除重复项,而且还希望在存在重复项的地方获得值的差异。 【参考方案1】:

使用下划线可以这样做:

var arr1 = [
  "lastInteracted": "2016-03-31T11:13:09.000Z",
  "email": "concierge@inbound.com",
  "interactionCount": 2
, 
  "lastInteracted": "2016-03-31T21:06:19.000Z",
  "email": "jbi@salesforce.com",
  "interactionCount": 1
, 
  "lastInteracted": "2016-03-29T11:15:41.000Z",
  "email": "abc@insightsquared.com",
  "interactionCount": 1
, 
  "lastInteracted": "2016-03-24T10:02:29.000Z",
  "email": "diana@hubspot.com",
  "interactionCount": 1
]
var arr2 = [
  "lastInteracted": "2016-03-31T11:13:09.000Z",
  "email": "concierge@inbound.com",
  "interactionCount": 2
, 
  "lastInteracted": "2016-03-31T21:06:19.000Z",
  "email": "jbi@salesforce.com",
  "interactionCount": 4
, 
  "lastInteracted": "2016-03-29T11:15:41.000Z",
  "email": "kstachowski@insightsquared.com",
  "interactionCount": 1
, 
  "lastInteracted": "2016-03-24T10:02:29.000Z",
  "email": "hammer@hubspot.com",
  "interactionCount": 1
, 
  "lastInteracted": "2016-03-24T10:02:29.000Z",
  "email": "life@hubspot.com",
  "interactionCount": 10
, 
  "lastInteracted": "2016-03-24T10:02:29.000Z",
  "email": "mike@hubspot.com",
  "interactionCount": 18
]

var ary = _.chain(arr1.concat(arr2))//use chain
  .groupBy(function(d) 
    return d.email;
  )//grouping by email
  .map(function(d) 
    var last = _.last(d);//take the last in the group
    var k = 
      email: last.email,
      lastInteracted: last.lastInteracted,
      interactionCount: _.reduce(d, function(memo, d1) 
        return memo + d1.interactionCount;//sum up interactionCount
      , 0)
    ;
    return k;
  ).value()

document.write('<pre>' + JSON.stringify(ary, 0, 4) + '</pre>');
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"&gt;&lt;/script&gt;

工作小提琴here

【讨论】:

Jsfiddle 不起作用。它只是打印库中的一些函数。 感谢您的快速更新。如果尝试减去interactionCounts,为什么它只是在interactionCounts的总和后面加上'-'? 我认为问题是关于添加interactionCounts 你为什么要减去? 我的错我完全错过了写预期的结果。我实际上应该得到两个数组的interactionCount 的差异。我需要跟踪这两个数组的interactionCount 是否增加或减少。 非常感谢@cyril!在我基本上改变了要求之后,我真的不敢相信你真的帮助了我。再次感谢 :-) :-)

以上是关于比较下划线js中的两个对象数组的主要内容,如果未能解决你的问题,请参考以下文章

比较两个对象数组,并将匹配值的元素排除到JS中的新数组中

js中判断对象是不是为数组的几种方式

mongoose 使用 lodash 或下划线 js 查询数组对象的数组

js 如何比较两个对象相等

js比较两个数组对象,取出不同的值

JS的对象比较,JS的数组比较