0

Possible Duplicate:
How to remove a property from an object?

I am comparing two JSON objects and then deleting the old items from a list with this code:

dangerousPeople = ({1:{title:"Jackie Chan", user:"Jackie"}, 2:{title:"Chuck Norris", user:"Chuck"}, 3:{title:"Britney spears", user:"Britney"}});
newDangerousPeople = ({1:{title:"Jackie Chan", user:"Jackie"}, 3:{title:"Britney spears", user:"Britney"}});

$.each(dangerousPeople, function(index)
{
    if(!newDangerousPeople[index]){

         $('#dangerousPeople #id'+index).slideUp("normal", function() { $(this).remove(); } );

         delete dangerousPeople.index;
    }
});

The part of the script that slidesup the element works, but deleting the element from the object I can't make it work.

I tried with delete dangerousPeople.index but doesn't work, also tried delete $(this) but no luck either.

So how should I remove the element from itself?

Community
  • 1
  • 1
Aleix
  • 489
  • 5
  • 16

1 Answers1

0

Try:

...

delete dangerousPeople[ index ];

...
David G
  • 94,763
  • 41
  • 167
  • 253
  • `index` is not an object, it's a variable holding a numeric string. – Felix Kling Nov 17 '12 at 22:27
  • In this case it's a primitive value. A string *can* be an object if you create it with `new String(...)`. Besides, it does not matter what value `index` contains. The point is that it is a variable and if you want to use a variable's value as property, you have to use bracket notation. – Felix Kling Nov 17 '12 at 22:29
  • @FelixKling: In C++, primitives are objects too, objects simply being regions of memory. Is this not the case in JS? – Lightness Races in Orbit Nov 17 '12 at 22:30
  • @LightnessRacesinOrbit: Well, of course primitives are also kept in memory, but but they are not objects (as in they inherit from `Object`). Simple example: `var foo = "42"; foo.bar = "bar"; alert(foo.bar);`. You cannot add properties to primitive values because they are not objects. Other languages like (as it seems) C++ and Java work differently. – Felix Kling Nov 17 '12 at 22:32
  • @FelixKling: OK - just didn't fancy looking up the definition of "object" in the rat's nest that is the ECMA spec. – Lightness Races in Orbit Nov 17 '12 at 22:37