Compare two object arrays and combine missing objects

1.6k Views Asked by At

I have 2 object arrays. The 1st is an array of managers. The 2nd is an array of selected managers from the 1st array. The difference is I added a property selected: true. I need to now replace the the managers in the first array with selected managers. I am doing this with an AngularJS service I created. I'm sure there is much simpler solution so I'm open to suggestions. JavaScript, jQuery, lodash, LINQ.js are good. I have a plunker and I have displayed the result I need. Notice the manager that does not have the selected:true property. screenshot

plunker

var app = angular.module("mainModule", []);
var MainController = function($scope, service) {

var eventUsers = [
  {
    "event_Users_ID":1009,"event_ID":11,"user_ID":"15e640c1-a481-4997-96a7-be2d7b3fcabb"
  },{
    "event_Users_ID":1010,"event_ID":11,"user_ID":"250a19be-e661-4c04-9a50-c84b0e7349b7"
  },{
   "event_Users_ID":1011,"event_ID":11,"user_ID":"4cada7f0-b961-422d-8cfe-4e96c1fc11dd"
  },{
   "event_Users_ID":1013,"event_ID":11,"user_ID":"a3125317-5deb-426d-bbb1-06d3bd4ebaa6"
  }];
 var managers = [
   {
    "id": "15e640c1-a481-4997-96a7-be2d7b3fcabb",
    "fullName": "Kul Srivastva"
   },{
    "id": "250a19be-e661-4c04-9a50-c84b0e7349b7",
    "fullName": "Todd Brothers"
   }, {
    "id": "4cada7f0-b961-422d-8cfe-4e96c1fc11dd",
    "fullName": "Rudy Sanchez"
   }, {
    "id": "79823c6d-de52-4464-aa7e-a15949fb25fb",
    "fullName": "Mike Piehota",
   }, {
    "id": "a3125317-5deb-426d-bbb1-06d3bd4ebaa6",
    "fullName": "Nick Broadhurst"
  }];                

   $scope.result = service.eventUserMatch(eventUsers, managers);
 };

  function service() {
    var vm = this;

    vm.eventUserMatch = function (eventUsers, managers) {
        var arry = [];
        arry = $.map(eventUsers, function (eventUser) {
            var manager = $.grep(managers, function (user) {
                return user.id === eventUser.user_ID;
            })[0];

            eventUser.id = manager.id;
            eventUser.fullName = manager.fullName;
            eventUser.selected = true;
            return eventUser;
        });
        return arry;
    };
 }

app.controller("MainController", MainController);
app.service('service', service);
4

There are 4 best solutions below

0
On BEST ANSWER

You can use Array#map.

// Get all the event user IDs in an array
var eventUserIds = eventUsers.map(e => e.user_ID);

// Iterate over managers
managers = managers.map(e => {
    // If manager is present in the event users, `select` it
    if (eventUserIds.indexOf(e.id) !== -1) {
        e.selected = true;
    }

    return e;
});

var eventUsers = [{
    "event_Users_ID": 1009,
    "event_ID": 11,
    "user_ID": "15e640c1-a481-4997-96a7-be2d7b3fcabb"
}, {
    "event_Users_ID": 1010,
    "event_ID": 11,
    "user_ID": "250a19be-e661-4c04-9a50-c84b0e7349b7"
}, {
    "event_Users_ID": 1011,
    "event_ID": 11,
    "user_ID": "4cada7f0-b961-422d-8cfe-4e96c1fc11dd"
}, {
    "event_Users_ID": 1013,
    "event_ID": 11,
    "user_ID": "a3125317-5deb-426d-bbb1-06d3bd4ebaa6"
}];


var managers = [{
    "id": "15e640c1-a481-4997-96a7-be2d7b3fcabb",
    "fullName": "Kul Srivastva"
}, {
    "id": "250a19be-e661-4c04-9a50-c84b0e7349b7",
    "fullName": "Todd Brothers"
}, {
    "id": "4cada7f0-b961-422d-8cfe-4e96c1fc11dd",
    "fullName": "Rudy Sanchez"
}, {
    "id": "79823c6d-de52-4464-aa7e-a15949fb25fb",
    "fullName": "Mike Piehota",
}, {
    "id": "a3125317-5deb-426d-bbb1-06d3bd4ebaa6",
    "fullName": "Nick Broadhurst"
}];

var eventUserIds = eventUsers.map(e => e.user_ID);
managers = managers.map(e => {
    if (eventUserIds.indexOf(e.id) !== -1) {
        e.selected = true;
    }

    return e;
})

console.log(managers);
document.getElementById('result').innerHTML = JSON.stringify(managers, 0, 4);
<pre id="result"></pre>

1
On

As you said, I do think there may be an easier way to do this.

I advise you to pick a look to SugarJs which is a JavaScript library that extends native objects with so helpful methods. In your case the doc on Arrays.

For me, it helps a lot dealing with a lot of native JavaScript Object (JSON).

2
On

would this work? Loop through the new array of managers, find the index using lodash of a matching manager object in the old manager array and replace it in the old manager array with the manager from the new manager array if found?

There's probably a more efficient way to write a solution to this but assuming I'm understanding your problem correctly I believe this should work? Can't test as I'm at work currently.

for(var i=0; i < SelectedManagersArray.length; i++){
    var index = _.findIndex(OldManagersArray, {id: SelectedManagersArray[i].id, fullName: selectedManagersArray[i].fullName);
//I believe lodash returns a -1 if a matching index isn't found.
    if(index !== -1){SelectedManagersArray[index] = OldManagersArray[i]}
    }
0
On

Simple implementation:

for(var i = 0; i < eventUsers.length; i++) {    
    for(var j = 0; j < managers.length; j++) {
        if(eventUsers[i].user_ID === managers[j].id) {
            managers[j].selected = true;
        }
    }
}