1

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.

ihojose
  • 312
  • 2
  • 12
  • 1
    Possible duplicate of [How do I remove an object from an array with JavaScript?](http://stackoverflow.com/questions/3396088/how-do-i-remove-an-object-from-an-array-with-javascript) – Pavneet_Singh Dec 28 '16 at 16:50

1 Answers1

4

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.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I'm not certain, but I have a feeling that the object will be in dictionary mode from the start since all the properties are dynamically added at runtime. I think the optimized objects are the ones with properties known at compile time, like `var o = {foo: "foo"}; o.bar = "bar"`. But I could be wrong about that. –  Dec 28 '16 at 16:57
  • @squint: No, *adding* properties (even unknown ones) is a common use case specifically catered for, even when their names aren't known until they're added: The engine (V8 at least) creates a new generated subclass and then associates that with the instance. But you can't *remove* a property in a subclass, hence falling back to dictionary mode when removing but not when adding. (I'm sure they could find a way, but the V8 author I chatted with about this a few years back basically said it just wasn't common enough to cater for.) – T.J. Crowder Dec 28 '16 at 17:02
  • Hmm... That could mean hundreds of thousands of V8's hidden classes in some cases just to represent a single object that is basically used as a hash table. I'd be surprised if they at least didn't have a very finite limitation to this optimization when it can be shown that all the object's keys are dynamic. But again, this is all just conjecture on my part. –  Dec 28 '16 at 17:09
  • @squint: Yes, IIRC there's a threshold where it just compiles a whole new class. I've never dived deep into the V8 source, though... :-) – T.J. Crowder Dec 28 '16 at 17:15
  • 1
    Just for reference, I found [your answer](http://stackoverflow.com/a/23456192/1106925) to a different question and took a look at the conversation you linked. It seems that Sven Panne [did suggest](https://groups.google.com/forum/#!msg/v8-users/zE4cOHBkAnY/TPjOqHpkz98J) elimination of that optimization when the object is a hash table. The *"...is very probably already in dictionary mode..."* part of his quote seems to line up with what you're saying, with the *"very probably"* being that mysterious threshold. Anyway, good information to have all around. –  Dec 28 '16 at 17:28
  • @squint: Wow. I would never have been able to find that. Nice one. – T.J. Crowder Dec 28 '16 at 17:47