1

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
    }
]
3
  • Have you tried this? stackoverflow.com/a/13514962/1702612 Commented Apr 12, 2016 at 7:51
  • Possible duplicate of Merging two collections using Underscore.JS Commented Apr 12, 2016 at 7: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. Commented Apr 12, 2016 at 8:04

1 Answer 1

1

Using underscore you can do it like this:

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

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

Sign up to request clarification or add additional context in comments.

5 Comments

The Jsfiddle doesn't work. It just prints some functions from the library.
Thanks for the quick update. If try to subtract interactionCounts, Why does it just append '-' to the sum of interactionCounts?
I think the question is about adding interactionCounts why are you subtracting?
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.
Thank you so much @cyril! I really can't believe you actually helped me after I basically changed the requirement. Thanks again :-) :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.