Skip to content

ConvertObjectKeys

Category
Last Changed
5月26日10时34分

Demo

Original Data1

{ "first_name": "John", "last_name": "Doe", "age": 30, "address": { "street_name": "Main St", "city": "Anytown", "state": "CA", "zip_code": "12345" } }

Converted to Camel Case

{ "firstName": "John", "lastName": "Doe", "age": 30, "address": { "street_name": "Main St", "city": "Anytown", "state": "CA", "zip_code": "12345" } }

Original Data2

{ "firstName": "Jane", "lastName": "Doe", "age": 25, "address": { "streetName": "Second St", "city": "Othertown", "state": "NY", "zipCode": "67890" } }

Converted to Snake Case

{ "first_name": "Jane", "last_name": "Doe", "age": 25, "address": { "streetName": "Second St", "city": "Othertown", "state": "NY", "zipCode": "67890" } }

Usage

ts
import { camelToSnake, convertObjectKeys } from 'comuse-shared'

const obj = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30
}

const result = convertObjectKeys(obj, camelToSnake) // { first_name: 'John', last_name: 'Doe', age: 30 }

Source

SourceDemoDocs

Changelog

v3.2.11 on 5/26/2025
105a0 - feat: add function methods convertObjectToKeys

Released under the MIT License.