tengo este objeto
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 } y quiero mutarlo para que la propiedad del proyecto anidado incluya la propiedad issueKey al final de la cadena, algo así como "Issues with this Issue Type must be created in the same project as the parent (JP-55)."
Sé que debería mostrar que lo intenté y lo hice, pero no he logrado llegar al principio de una solución.
El objeto final se vería así:
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 }Muchas gracias
Desea calcular una nueva propiedad en función de otros valores. Solo intenta acceder y anularlo.
Si siempre tiene un punto al final, puede usar una combinación de split() y 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); PD: el código anterior se romperá si no hay .
Puede usar el operador += para 'agregar' algo al final de la cadena.
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);