I had an object like this (fields in snake_case)
const obj = {
vt_core_random: {
user_details: {
first_name: "xyz",
last_name: "abc",
groups: [
{
id: 1,
group_type: "EXT"
},
{
id: 2,
group_type: "INT"
}
],
address_type: {
city_name: "nashik",
state: {
code_name: "MH",
name: "Maharashtra"
}
}
}
}
};
I want to recursily convert its fields to camelCase, so the expected output is given below
const obj = {
vtCoreRandom: {
userDetails: {
firstName: "xyz",
lastName: "abc",
groups: [
{
id: 1,
groupType: "EXT"
},
{
id: 2,
groupType: "INT"
}
],
addressType: {
cityName: "LMN",
state: {
codeName: "KOP",
name: "PSQ"
}
}
}
}
};
I tried using mapKeys() but I just can't wrap my head around the recursive part of this. any help is highly appreciated. Also I have the ability to use lodash
if it makes the process any simpler
import { camelCase, isArray, transform, isObject } from "lodash"; const camelize = (obj: Record<string, unknown>) => transform(obj, (result: Record<string, unknown>, value: unknown, key: string, target) => { const camelKey = isArray(target) ? key : camelCase(key); result[camelKey] = isObject(value) ? camelize(value as Record<string, unknown>) : value; });
– Mercator