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

183
Visualizações
How to find and remove all underscores, and make the next letter after the underscore uppercase

i have object with underscores keys

const obj = { api_key: 1, is_auth: false }

i want to get camelCase keys

const obj = { apiKey: 1, isAuth: false }

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

RegEx can change the casing, and then the converted keys must be put in the object:

const obj = { api_key: 1, is_auth: false } // your sample data

for(let key in obj) {
    let newKey = key.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
    if(newKey!=key) {
        obj[newKey] = obj[key];
        delete obj[key];
    }
}
      

The inner if takes care of preserving any key in obj that should not follow Snake Case.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can map the keys using a regular expression to replace the underscores and following character with an uppercased character. Something like:

const obj = {
  api_key: 1,
  is_true: false,
  all___under_scores: true,
  _test: "Tested",
  test_: "TestedToo (nothing to replace)",
};
const keys2CamelCase = obj => Object.fromEntries(
  Object.entries(obj)
  .map(([key, value]) => 
    [key.replace(/_{1,}([a-z])/g, (a, b) => b.toUpperCase()), value])
);

console.log(keys2CamelCase(obj));

If you also want to remove trailing underscores use

key.replace(/_{1,}([a-z])|_$/g, (a, b) => b && b.toUpperCase() || ``)
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