简体   繁体   English

从关联数组中删除键

[英]Delete a key from an associative array

Consider var person=JSON.parse('{"name":"Alice","id",1234}') . 考虑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} ? 例如,如何完全删除“名称”,以便该person成为{"id":1234}

Try delete person["name"] . 尝试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. 请注意, delete只会将其设置为undefined,因此不会在数组的长度中正确反映出来。

If you know the key you should use splice ie 如果知道密钥,则应使用拼接,即

myArray.splice(key, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM