I have two arrays of objects
var arr1 =
[
{
"lastInteracted": "2016-03-31T11:13:09.000Z",
"email": "[email protected]",
"interactionCount": 2
},
{
"lastInteracted": "2016-03-31T21:06:19.000Z",
"email": "[email protected]",
"interactionCount": 1
},
{
"lastInteracted": "2016-03-29T11:15:41.000Z",
"email": "[email protected]",
"interactionCount": 1
},
{
"lastInteracted": "2016-03-24T10:02:29.000Z",
"email": "[email protected]",
"interactionCount": 1
}
]
and
var arr2 =
[
{
"lastInteracted": "2016-03-31T11:13:09.000Z",
"email": "[email protected]",
"interactionCount": 2
},
{
"lastInteracted": "2016-03-31T21:06:19.000Z",
"email": "[email protected]",
"interactionCount": 4
},
{
"lastInteracted": "2016-03-29T11:15:41.000Z",
"email": "[email protected]",
"interactionCount": 1
},
{
"lastInteracted": "2016-03-24T10:02:29.000Z",
"email": "[email protected]",
"interactionCount": 1
},
{
"lastInteracted": "2016-03-24T10:02:29.000Z",
"email": "[email protected]",
"interactionCount": 10
},
{
"lastInteracted": "2016-03-24T10:02:29.000Z",
"email": "[email protected]",
"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": "[email protected]",
"interactionCount": 0
},
{
"lastInteracted": "2016-03-31T21:06:19.000Z",
"email": "[email protected]",
"interactionCount": -4
},
{
"lastInteracted": "2016-03-29T11:15:41.000Z",
"email": "[email protected]",
"interactionCount": 1
},
{
"lastInteracted": "2016-03-24T10:02:29.000Z",
"email": "[email protected]",
"interactionCount": 1
},
{
"lastInteracted": "2016-03-29T11:15:41.000Z",
"email": "[email protected]",
"interactionCount": 1
},
{
"lastInteracted": "2016-03-24T10:02:29.000Z",
"email": "[email protected]",
"interactionCount": 1
},
{
"lastInteracted": "2016-03-24T10:02:29.000Z",
"email": "[email protected]",
"interactionCount": 10
},
{
"lastInteracted": "2016-03-24T10:02:29.000Z",
"email": "[email protected]",
"interactionCount": 18
}
]