简体   繁体   English

如何从 object 中删除属性但保留其值?

[英]How can i remove a property from an object but keep its values?

I would like to know how can I remove the "formvalue1" property from the following object but keep its children.我想知道如何从以下 object 中删除"formvalue1"属性,但保留其子项。

{
  "formvalue1": {
    "title": "sdf",
    "tname": "sdff",
    "taddress": "dfsdf"
  }
}

Try reassigning the object:尝试重新分配 object:

let obj = {"formvalue1": { "title":"sdf", "tname":"sdff", "taddress":"dfsdf" } };
obj = obj.formvalue1;
console.log(obj)

If the original JSON is a string and not a JSON object.如果原始 JSON 是字符串而不是 JSON object。 In javascript you can use JSON.parse to parse the string into an object literal (see comment below pointing out the discrepancy).在 javascript 中,您可以使用JSON.parse将字符串解析为 object 文字(请参阅下面指出差异的注释)。

const jsonStr = '{"formvalue1": {"title": "sdf", "tname": "sdff", "taddress": "dfsdf"}}'
let obj = JSON.parse(jsonStr);
obj = obj.formvalue1;
console.log(obj)

you use some destructuring你使用了一些destructuring

 const d = { "formvalue1": { "title": "sdssssssf", "tname": "sdff", "taddress": "dfsdf" } } const { formvalue1 } = d const nd = formvalue1 console.log(nd)

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

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