1

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);
Shaohao
  • 3,471
  • 7
  • 26
  • 45
texas697
  • 5,609
  • 16
  • 65
  • 131
  • Very confusing problem description. What is higher level use case and problem you are trying to solve? – charlietfl Jan 29 '16 at 13:49
  • What have you tried? What about the manager without selected:true????? You say "Notice the manager that does not have the 'selected:true' property " yet not explain what's this about. – RaidenF Jan 29 '16 at 13:49
  • yea, sorry i have hard time searching for help because i cant explain well. give me a minute and i will explain overall situation – texas697 Jan 29 '16 at 13:50
  • I think I see what you're trying to accomplish....could you loop through the new array of Selected Managers and use lodash's findByIndex method to search for each manager in the Selected Manager Array in the first array of managers and if the index is found, replace the manager in the first array with the manager currently targeted by the loop in the second array? https://lodash.com/docs#findIndex – Cameron Billings Jan 29 '16 at 13:53
  • As far as I understand what you want to achieve: cycle through `eventUsers`, if `eventUsers[i].user_ID` equals `managers[j].id` then add property `"selected" : true` to `managers[j]`. Right? – Vlad Zhukov Jan 29 '16 at 13:55
  • take a look at update – texas697 Jan 29 '16 at 13:57
  • @Freeeeez yes, exactly that – texas697 Jan 29 '16 at 13:57

4 Answers4

2

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>
Tushar
  • 85,780
  • 21
  • 159
  • 179
1

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).

Tushar
  • 85,780
  • 21
  • 159
  • 179
Ganov13
  • 367
  • 2
  • 10
  • @Tushar new on StackOverflow, thx for help editing answers, appreciate! Noted for next time (noise reduction; layout ;) ) – Ganov13 Jan 29 '16 at 14:49
1

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]}
    }
  • Could actually be simpler by using a method like Freeeeez suggested and just finding the index like I wrote in my code and basically saying if(index !== 1){SelectedManagersArray[index].selected = true}. Could be a better approach because replacing objects with other objects from different places can get messy due to passing by reference as opposed to value. – Cameron Billings Jan 29 '16 at 14:04
  • 1
    If you use lodash, then use it everywhere, e.g. `forEach` instead of `for`. On the other hand it's pointless to use such a huge library just for few strings of code. – Vlad Zhukov Jan 29 '16 at 14:05
1

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;
        }
    }
}
Vlad Zhukov
  • 143
  • 10