簡體   English   中英

變異嵌套深度 object

[英]Mutate nested deep object

我有這個 object

const a = {
  elementErrors: {
    errorMessages: [],
    errors: {
      project: "Issues with this Issue Type must be created in the same project as the parent."
    }
  },
  issueKey: "JP-55",
  status: 400
}

我想改變它,以便嵌套的項目屬性在字符串的末尾包含 issueKey 屬性,例如"Issues with this Issue Type must be created in the same project as the parent (JP-55)."

我知道我應該證明我已經嘗試過並且已經做到了,但是我還沒有設法開始解決方案。

最終的 object 看起來像這樣:

const a = {
      elementErrors: {
        errorMessages: [],
        errors: {
          project: "Issues with this Issue Type must be created in the same project as the parent (JP-55)."
        }
      },
      issueKey: "JP-55",
      status: 400
    }

非常感謝

您想要根據其他一些值計算新屬性。 只需嘗試訪問並覆蓋它。

如果最后總是有一個點,則可以混合使用split()template literals

 const a = { elementErrors: { errorMessages: [], errors: { project: "Issues with this Issue Type must be created in the same project as the parent." } }, issueKey: "JP-55", status: 400 } a.elementErrors.errors.project = `${a.elementErrors.errors.project.split('.')[0]} (${a.issueKey}).`; console.log(a);

PS:如果沒有 上面的代碼將中斷.

您可以使用+=運算符將某些內容“添加”到字符串的末尾。

a.elementErrors.errors.project += ` (${a.issueKey})`;

 let a = { elementErrors: { errorMessages: [], errors: { project: "Issues with this Issue Type must be created in the same project as the parent." } }, issueKey: "JP-55", status: 400 } a.elementErrors.errors.project += ` (${a.issueKey})`; console.log(a);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM