删除JSON属性[重复]

本文翻译自: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. 用[]表示法替换点符号,以便在删除之前评估那些值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值