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

178
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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