Skip to content

camelCaseKeys

Recursively transforms every key of a plain object (including keys nested inside arrays and nested objects) to camelCase. Handles snake_case, kebab-case, PascalCase, and space-separated keys. Non-plain-object values (arrays’ items aside) — Date, Map, Set, class instances, primitives — are left untouched, only their position is walked into. An entry whose transformed key is a prototype-polluting string (__proto__, constructor, prototype) is silently skipped, same as the rest of @helpers4/object.

Available since v3.0.3

import { camelCaseKeys } from '@helpers4/object';
// or, from the all-in-one package (same code, one install):
import { camelCaseKeys } from 'helpers4/object';
camelCaseKeys<T>(value: T): T
ParameterTypeDescription
valueTThe object (or array of objects) to transform

T — A new value with every plain-object key converted to camelCase

Recursively converts every key, including nested objects, to camelCase.

camelCaseKeys({ user_name: 'Alice', home_address: { zip_code: '12345' } })
// => { userName: 'Alice', homeAddress: { zipCode: '12345' } }

Each object inside an array gets its keys converted.

camelCaseKeys({ user_list: [{ first_name: 'A' }] })
// => { userList: [{ firstName: 'A' }] }

View source on GitHub