我有這個物件
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)."
我知道我應該表明我嘗試過并且我做到了,但我還沒有設法開始解決方案。
最終物件將如下所示:
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
}
非常感謝
uj5u.com熱心網友回復:
您可以使用 =
運算子將??某些內容“添加”到字串的末尾。
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);
uj5u.com熱心網友回復:
您想根據其他一些值計算新屬性。只需嘗試訪問并覆寫它。
如果末尾總是有一個點,則可以混合使用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:如果沒有,上面的代碼會中斷.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/467539.html
標籤:javascript 打字稿 目的
上一篇:去掉數字開頭的符號-JS
下一篇:如何在箭頭函式中添加if陳述句