KalEl
KalEl

Reputation: 9136

Delete a key from an associative array

Consider var person=JSON.parse('{"name":"Alice","id",1234}').

How do I remove a key from the variable person? For example, how do I remove "name" completely, so that person becomes {"id":1234}?

Upvotes: 7

Views: 16665

Answers (1)

Konamiman
Konamiman

Reputation: 50313

Try delete person["name"].

Notice that delete will only set it as undefined, which will then not be reflected correctly in the length of the array.

If you know the key you should use splice i.e.

myArray.splice(key, 1);

Upvotes: 20

Related Questions