7

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}?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
KalEl
  • 8,978
  • 13
  • 47
  • 56
  • This is not an associative array. It is an object literal. There is no `length` property like with an array (unless of course you define one). – Justin Johnson Nov 18 '09 at 10:03
  • possible duplicate of [How do I remove objects from a javascript associative array?](http://stackoverflow.com/questions/346021/how-do-i-remove-objects-from-a-javascript-associative-array) – Bennor McCarthy Feb 28 '13 at 12:16

1 Answers1

20

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);

Machado
  • 8,965
  • 6
  • 43
  • 46
Konamiman
  • 49,681
  • 17
  • 108
  • 138