back to series

Implement a camelCaseKeys function

camelCase is a convention used in programming and naming conventions where multiple words are combined into one continuous string of text, with each word within the string capitalized except for the first word.

camelCase conventions are followed in JavaScript codebases as well. We need to implement a camelCaseKeys functon that can convert all keys in object to follow camelCase convention.

1. Basic implementation

Input can have nested objects or array of objects. Try not to mutate the original input.

./src/camelCaseKeys.js
import camelCase from './src/utils.js';
function camelCaseKeys(input) {
// for arrays
if (Array.isArray(input)) {
const outArr = [];
input.forEach((item) => {
outArr.push(camelCaseKeys(item));
});
return outArr;
}
// for objects
const outObj = Object.create({});
for (const key in input) {
// avoid iterating over inherited keys
if (Object.hasOwn(input, key)) {
const value = input[key];
if (typeof value !== 'object') {
outObj[camelCase(key)] = value;
} else {
outObj[camelCase(key)] = camelCaseKeys(value);
}
}
}
return outObj;
}
// Usage
const convertedObj = camelCaseKeys({
hello_world: true,
hello: {
baz_bar: [{ name: 'JS', age_in_years: 24 }]
}
});
console.log(convertedObj);
/**
* Yields
* {
"helloWorld": true,
"hello": {
"bazBar": [
{
"name": "JS",
"ageInYears": 24
}
]
}
}
*/

Here the implementation of camelCase is not really important. You can use any technique solution you prefer present on the internet. The goal is not to focus on utility but rather the recursion done.

Further Reading

I strongly encourage you to explore and tackle additional questions in my Recursion Questions for Frontend Interviews blog series.

By doing so, you can enhance your understanding and proficiency with recursion, preparing you to excel in your upcoming frontend interviews.

Wishing you best. Happy Interviewing 🫡.