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_namebecomesfirstName,user-idbecomesuserId,a__b-cbecomesaBC._idandcount_stay_idandcount_.alreadyCamelandNameare 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
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 11. ⌘Enter runs, ⌘⇧Enter submits.