javascript - 比较 underscorejs 中的两个对象数组

标签 javascript arrays underscore.js lodash

我有两个对象数组

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 与 arr2 的 interactionCount 进行比较,否则返回 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
    }
]

最佳答案

使用下划线你可以这样做:

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>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

工作 fiddle here

关于javascript - 比较 underscorejs 中的两个对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36566877/

相关文章:

javascript - 使用 JavaScript 中的变量创建自定义 URL

underscore.js - 将 _.filter 和 _.reject 作为单个操作运行,就像 Haskell 的 Data.List.Partition

javascript - 我扩展了 CSSStyleDeclaration.prototype,如何访问 'parent'

javascript - Sigma.js 上的大型数据集

javascript - 如何在 doPost() 中的 servlet 内从 JavaScript 读取传入的 JSON?

arrays - swift 代码 : how do you build a Dictionary of Array of String based on Dictionary entry being nil

javascript - jQuery Grails 插件

c - C语言中删除字符串中的字母

javascript - jQuery 缩放 div 到父级

javascript - Underscore.js 将对象键展平为数组