Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 406
KalEl Avatar asked Nov 18 '09 09:11

KalEl


1 Answers

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

like image 171
Konamiman Avatar answered Oct 20 '22 19:10

Konamiman