Remove Objects From Array Based On Keys & Properties In Another Object?
I have the following object, where the property key is set to a username and the property value is set to a count of the number of times they appear, and an array containing a list
Solution 1:
I've tried varies permutations and loops of
.filter
and.splice
, but with no success.
You will only need filter
:
return siteUsers.filter(function(obj) {
var user = obj.UserName;
return !(user in peopleWhoAppear && peopleWhoAppear[user] == count);
});
This will get you a new array, with all the users that did not appear in peoplWhoAppear
or had the wrong count in there.
Solution 2:
Filter()
doesn't change the array, it returns a new one. If you want to iteratively filter the array, you have to set it to the return value of filter()
each time.
// copy the arrayvar newSiteUsers = siteUsers.slice(0)
for (varUserNamein peopleWhoAppear) {
if (peopleWhoAppear[UserName ] === count) {
newSiteUsers = newSiteUsers.filter(function (person) {
console.log(person);
return person.UserName !== UserName;
}));
}
}
console.log(newSiteUsers);
Post a Comment for "Remove Objects From Array Based On Keys & Properties In Another Object?"