繁体   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