【问题标题】:Compare two arrays of objects in underscorejs比较下划线js中的两个对象数组
【发布时间】: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
    }
]

【问题讨论】:

标签: javascript arrays underscore.js lodash


【解决方案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!在我基本上改变了要求之后,我真的不敢相信你真的帮助了我。再次感谢 :-) :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多