1

I have two arrays of objects

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
    }
    ]

and

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
    }
]

I want to merge these two arrays such that if the email of an object exists in both then diff the interactionCount from arr1 with arr2 else return the interactionCount of either arr1 or arr2.

Result will be

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
    }
]
Naveen Paul
  • 454
  • 8
  • 18
  • Have you tried this? http://stackoverflow.com/a/13514962/1702612 – Bikas Apr 12 '16 at 07:51
  • Possible duplicate of [Merging two collections using Underscore.JS](http://stackoverflow.com/questions/13514121/merging-two-collections-using-underscore-js) – alexmac Apr 12 '16 at 07:56
  • I have gone through those solutions but they don't do what I need. If you can look the results array, maybe it will be much more clearer as I don't only want to eliminate duplicates but also get a diff of the values wherever the duplicates are present. – Naveen Paul Apr 12 '16 at 08:04

1 Answers1

1

Using underscore you can do it like this:

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>

working fiddle here

Cyril Cherian
  • 32,177
  • 7
  • 46
  • 55
  • The Jsfiddle doesn't work. It just prints some functions from the library. – Naveen Paul Apr 12 '16 at 10:37
  • Thanks for the quick update. If try to subtract interactionCounts, Why does it just append '-' to the sum of interactionCounts? – Naveen Paul Apr 12 '16 at 10:50
  • I think the question is about adding interactionCounts why are you subtracting? – Cyril Cherian Apr 12 '16 at 10:52
  • My bad I completely missed writing the the expected results. I should actually be getting a diff of the two array's interactionCount. I need to track if the interactionCount have increased or decreased over those two arrays. – Naveen Paul Apr 12 '16 at 10:58
  • Thank you so much @cyril! I really can't believe you actually helped me after I basically changed the requirement. Thanks again :-) :-) – Naveen Paul Apr 12 '16 at 11:25