Delete a key from an associative array

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-31 09:34:34

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/1754777/delete-a-key-from-an-associative-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!