从关联数组中删除一个键 [英] Delete a key from an associative array

查看:46
本文介绍了从关联数组中删除一个键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如何从变量 person 中删除密钥?例如,如何彻底删除name",使 person 变成 {"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}?

推荐答案

尝试 delete person["name"].

请注意,delete 只会将其设置为未定义,这将不会正确反映在数组的长度中.

Notice that delete will only set it as undefined, which will then not be reflected correctly in the length of the array.

如果你知道密钥,你应该使用 splice,即

If you know the key you should use splice i.e.

myArray.splice(key, 1);

这篇关于从关联数组中删除一个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆