I'm trying to remove objects from an array based on a key/value combination - in my case to remove all "non-active" users.
Example code looks like
var items = [
{ "userID":"694","active": false },
{ "userID":"754","active": true },
{ "userID":"755","active": true },
{ "userID":"760","active": false },
{ "userID":"761","active": false },
{ "userID":"762","active": false }
]
function removeByKey(array, params){
array.some(function(item, index) {
return (array[index][params.key] !== params.value) ? !!(array.splice(index, 1)) : false;
});
return array;
}
for (var i = 0; i < items.length; i++){
var removed = removeByKey(items, {
key: 'active',
value: true
});
}
console.log(removed);
But each time when last entry in the array contains "active": false
, it will not be removed.
Any help is appreciated!