Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I'm using Angular. I'm trying to compare two arrays of objects I was able to get it working doing it like this:

  var compareUsers = function () {
    //Comparing assigned groups with all to return available users
    var assignedUsersIds = {};
    var usersIds = {};
    availableUsers = []; //declared higher up, populated with user objects on load

    //assignedUsers, declaired higher up, populated by function calling compareUsers
    assignedUsers.forEach(function (el, i) {
      assignedUsersIds[el.id] = assignedUsers[i];
    });

    allUsers.forEach(function (el, i) {
      usersIds[el.id] = allUsers[i];
    });

    for (var i in usersIds) {
        if (!assignedUsersIds.hasOwnProperty(i)) {
            availableUsers.push(usersIds[i]);
        }
    };
    console.log(availableUsers);
    return availableUsers;
  }

I found a better way to do it so I refactored it to this, using lodash:

  var compareUsers = function () {
    availableUsers = _.filter(allUsers, function(user){
      return !_.findWhere(assignedUsers, user);
    });
    console.info(availableUsers);
    return availableUsers;
  }

However, I'm not getting the correct results and not sure what I messed up. The new methods returns availableUsers which are in the assignedUsers list for some groups. The first time it runs, it seems to work but if I keep changing what group i'm looking at the results are all off and don't add up.

I found this method here.

share|improve this question
up vote 1 down vote accepted

In the first example you are using Array.push, which is always adding up (obviously), no matter how often you call compareUsers.

In the second example you are overwriting availableUsers each time you are calling compareUsers with the result of _.filter

I'd suggest that instead of doing:

availableUsers = _.filter(allUsers, function(user){
    return !_.findWhere(assignedUsers, user);
});

you do:

availableUsers = availableUsers.concat(_.filter(allUsers, function(user){
    return !_.findWhere(assignedUsers, user);
}));

This should work. It will concat the availableUsers array with the result of _.filter.

share|improve this answer
    
Hey so I just tried it. At first it said couldn't concat undefined which is likely because I didn't declare it to be an array. So at the very top of the function I set availableUsers = []; then what you suggested but now it's just returning an empty array – Batman 23 hours ago
    
This might be related stackoverflow.com/questions/7962953/… – Batman 22 hours ago
    
It will not work if you are setting it as an empty array at the top of the function. In the first example you comment "declared higher up". You should also declare the array outside the function in the second example. I think your lodash filter function is not doing what you want it to do. – FlorianTopf 22 hours ago
    
I edited the answer. You are right. It is related to the SO question you posted. – FlorianTopf 22 hours ago
    
It's running but I'm still getting the same issue as before. I'm going to sleep for now, I'll have to get back to this tomorrow. I'll update if i figure it out. – Batman 22 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.