Naveen Paul
Naveen Paul

Reputation: 454

Compare two arrays of objects in underscorejs

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

Upvotes: 1

Views: 739

Answers (1)

Cyril Cherian
Cyril Cherian

Reputation: 32327

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

Upvotes: 1

Related Questions