簡體   English   中英

如何從 object 中刪除屬性但保留其值?

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

我想知道如何從以下 object 中刪除"formvalue1"屬性,但保留其子項。

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

嘗試重新分配 object:

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

如果原始 JSON 是字符串而不是 JSON object。 在 javascript 中,您可以使用JSON.parse將字符串解析為 object 文字(請參閱下面指出差異的注釋)。

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

你使用了一些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