Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mutate nested deep object

I have this 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
}

and I want to mutate it so that the nested project property includes the issueKey property at the end of the string, something like "Issues with this Issue Type must be created in the same project as the parent (JP-55)."

I know I should show that I tried and I did but I haven’t managed to come with the beginning of a solution.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The final object would look like this:

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
    }

Many thanks

>Solution :

You can use the += operator to ‘add’ something to the end of the string.

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading