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