Back to Es Toolkit

toKebabCaseKeys

docs/reference/object/toKebabCaseKeys.md

1.51.01.9 KB
Original Source

toKebabCaseKeys

Returns a new object with all keys in objects and arrays converted to kebab-case.

Kebab case is a naming convention where each word is written in lowercase and connected with dashes (-). For example, it's written as kebab-case.

typescript
const kebabCased = toKebabCaseKeys(obj);

Usage

toKebabCaseKeys(obj)

Use toKebabCaseKeys when you want to convert all keys of an object to kebab-case. Nested objects and objects within arrays are also converted recursively.

For example, object keys are converted as follows:

  • camelCasekebab-case (e.g. userIduser-id)
  • PascalCasekebab-case (e.g. UserIduser-id)
  • UPPERCASE_KEYSkebab-case (e.g. FIRST_NAMEfirst-name, LASTlast)
typescript
import { toKebabCaseKeys } from 'es-toolkit/object';

// Basic object conversion
const obj = { userId: 1, firstName: 'John', lastName: 'Doe' };
const result = toKebabCaseKeys(obj);
// result is { 'user-id': 1, 'first-name': 'John', 'last-name': 'Doe' }

// Objects within arrays are also converted
const users = [
  { userId: 1, firstName: 'John' },
  { userId: 2, firstName: 'Jane' },
];
const convertedUsers = toKebabCaseKeys(users);
// convertedUsers is [{ 'user-id': 1, 'first-name': 'John' }, { 'user-id': 2, 'first-name': 'Jane' }]

// Nested objects are fully converted
const nested = {
  userData: {
    userId: 1,
    contactInfo: {
      emailAddress: '[email protected]',
      phoneNumber: '123-456-7890',
    },
  },
};
const nestedResult = toKebabCaseKeys(nested);
// nestedResult is {
//   'user-data': {
//     'user-id': 1,
//     'contact-info': {
//       'email-address': '[email protected]',
//       'phone-number': '123-456-7890'
//     }
//   }
// }

Parameters

  • obj (T): The object, array, or primitive value to convert keys to kebab-case.

Returns

(ToKebabCaseKeys<T>): Returns a new object with all keys converted to kebab-case.