Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda