For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/object/toConstantCaseKeys.md.

toConstantCaseKeys es-toolkit

Returns a new object with all keys in objects and arrays converted to CONSTANT_CASE.

Constant case is a naming convention where each word in an identifier is written in uppercase and connected with underscores (_). For example, it's written as CONSTANT_CASE.

const constantCased = toConstantCaseKeys(obj);

Usage

toConstantCaseKeys(obj)

Use toConstantCaseKeys when you want to convert all keys of an object to CONSTANT_CASE. Nested objects and objects within arrays are also converted recursively.

For example, object keys are converted as follows:

  • camelCaseCONSTANT_CASE (e.g. userIdUSER_ID)
  • PascalCaseCONSTANT_CASE (e.g. UserIdUSER_ID)
  • snake_caseCONSTANT_CASE (e.g. first_nameFIRST_NAME, lastLAST)
import { toConstantCaseKeys } from 'es-toolkit/object';

// Basic object conversion
const obj = { userId: 1, firstName: 'John', lastName: 'Doe' };
const result = toConstantCaseKeys(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 = toConstantCaseKeys(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 = toConstantCaseKeys(nested);
// nestedResult is {
//   USER_DATA: {
//     USER_ID: 1,
//     CONTACT_INFO: {
//       EMAIL_ADDRESS: '[email protected]',
//       PHONE_NUMBER: '123-456-7890'
//     }
//   }
// }

Parameters

objTrequired

The object, array, or primitive value to convert keys to CONSTANT_CASE.

Returns

Returns a new object with all keys converted to CONSTANT_CASE.

ToConstantCaseKeys<T>
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.