开发者

Delete a key from an associative array

开发者 https://www.devze.com 2022-12-12 07:06 出处:网络
Consider var person=JSON.parse(\'{\"name\":\"Alice\",\"id\",1234}\'). How do I remove a key from the variable person? For example, how do I re开发者_StackOverflow中文版move \"name\" completely, so th

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

How do I remove a key from the variable person? For example, how do I re开发者_StackOverflow中文版move "name" completely, so that person becomes {"id":1234}?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消