Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!