Skip to content
Onsite

Convert Object Keys to camelCase

MediumP3Coding

Implement camelCaseKeys(value), which returns a copy of value with every object key converted to camelCase, at any depth, including objects inside arrays.

Key rule: in a key, every run of underscores or hyphens that sits between other characters is removed and the character after it is upper-cased. All other characters, including the case of the first character, stay as they are. Leading and trailing separators are kept.

  • first_name becomes firstName, user-id becomes userId, a__b-c becomes aBC.
  • _id and count_ stay _id and count_.
  • alreadyCamel and Name are unchanged.

Only plain objects and arrays are traversed. Values (strings, numbers, null, Date and so on) are kept unchanged, and the input is not modified.

Example 1

Input
camelCaseKeys({ first_name: 'Ada', pet_list: [{ pet_name: 'x' }] })
Output
{ firstName: 'Ada', petList: [{ petName: 'x' }] }

Constraints

  • Total number of keys <= 10^4
  • No circular references.

Hints

Run checks the 3 visible tests, Submit all 11.