Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
Recursively convert an object fields from camelCase to UPPERCASE

I have tried to recursively convert an object fields from camelCase to UPPERCASE.

For some reason it wont work, I have tried many different ways from here in stackoverflow.

Would apprecaite for all the help, thanks.

 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));
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can recursively loop over the object and transform the keys to be uppercase.

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));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda