He intentado convertir recursivamente los campos de un objeto de camelCase a MAYÚSCULAS.
Por alguna razón, no funcionará, he intentado muchas formas diferentes desde aquí en stackoverflow.
Agradecería toda la ayuda, gracias.
const o = { KeyFirst: "firstVal", KeySecond: [ { KeyThird: "thirdVal", }, ], KeyFourth: { KeyFifth: [ { KeySixth: "sixthVal", }, ], }, }; function renameKeys(obj) { return Object.keys(obj).reduce((acc, key) => { const value = obj[key]; const modifiedKey = `${key[0].toLowerCase()}${key.slice(1)}`; if (Array.isArray(value)) { return { ...acc, ...{ [modifiedKey]: value.map(renameKeys) }, }; } else if (typeof value === "object") { return renameKeys(value); } else { return { ...acc, ...{ [modifiedKey]: value }, }; } }, {}); } console.log(renameKeys(o));Puede recorrer recursivamente el objeto y transformar las claves para que estén en mayúsculas.
const o = { KeyFirst: { KeySecond: "secondVal" }, KeyThird: [{ KeyFourth: "fourthVal" }], KeyFifth: { KeySixth: [{ KeySeventh: "seventhVal" }], }, }; function renameKeys(obj) { if (Array.isArray(obj)) { return obj.map((o) => renameKeys(o)); } else if (typeof obj === "object" && obj !== null) { return Object.entries(obj).reduce( (r, [k, v]) => ({ ...r, [k.toUpperCase()]: renameKeys(v) }), {} ); } else { return obj; } } console.log(renameKeys(o));