I have a list of users that I want to filter, using _.difference. But it is not working on comparing the objects. It only works if I compare only the usernames. Here is the code snippet:
var users = [
{
"username": "user1",
"birthdate": "birth1"
},
{
"username": "user2",
"birthdate": "birth2"
},
{
"username": "user3",
"birthdate": "birth3"
},
{
"username": "user4",
"birthdate": "birth4"
}
];
var keep = [
{
"username": "user1",
"birthdate": "birth1"
},
{
"username": "user3",
"birthdate": "birth3"
}
];
log(_.difference(_.pluck(users,"username"),_.pluck(keep,"username"))); // works
log(_.difference(users,keep)); // this is what I want, does not work
Any idea? Thanks - C.
Note: an alternative way of doing it is as followss, but not sure about the efficiency:
log( _.filter(users, function(num){
return (!_.contains(_.pluck(keep,"username"),num.username))
}) );