Reputation: 311
My obejct array is this
var items = {};
$http({
url: 'http://service.com/api/notifications',
method: 'GET',
params: {},
headers: {}
}).then(function (response) {
for (var i = 0; i < response.data.length; i++) {
item[response.data[i].id] = response.data[i];
}
});
Result is similar to this:
{
120: {
"id": 120,
"name": "John Doe",
"rol": "admin"
},
300: {
"id": 120,
"name": "Saya",
"rol": "moderator"
}
}
I do this so that there are no repeated items when I print them, because the service is re-consulted from time to time.
I need to delete an item from this array.
Upvotes: 1
Views: 120
Reputation: 1075587
I need to delete an item from this array.
It's not an array, it's an object. You can remove a property from an object using the delete
operator:
delete theObject[120];
// or
delete theObject["120"];
...will remove the "120"
property from it, releasing that object.
It almost never matters, but just be aware that removing a property from an object on most modern JavaScript engines impacts the subsequent performance of looking up properties on that object, since they usually start out as optimized lookups but then fall back into "dictionary mode" when you remove (rather than adding) properties. More in this answer. Again, though, it almost never matters.
Upvotes: 4