本文翻译自:Remove a JSON attribute [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
if I have a JSON object say: 如果我有一个JSON对象说:
var myObj = {'test' : {'key1' : 'value', 'key2': 'value'}}
can I remove 'key1' so it becomes: 我可以删除'key1',因此它变为:
{'test' : {'key2': 'value'}}
#1楼
参考:https://stackoom.com/question/57HS/删除JSON属性-重复
#2楼
简单:
delete myObj.test.key1;
#3楼
The selected answer would work for as long as you know the key itself that you want to delete but if it should be truly dynamic you would need to use the [] notation instead of the dot notation. 只要您知道要删除的键本身,所选答案就会起作用,但如果它应该是真正动态的,则需要使用[]表示法而不是点表示法。
For example: 例如:
var keyToDelete = "key1";
var myObj = {"test": {"key1": "value", "key2": "value"}}
//that will not work.
delete myObj.test.keyToDelete
instead you would need to use: 相反,你需要使用:
delete myObj.test[keyToDelete];
Substitute the dot notation with [] notation for those values that you want evaluated before being deleted. 用[]表示法替换点符号,以便在删除之前评估那些值。