← JavaScript Coding

Camel Case Keys

mediumobjectsstringsrecursion
Asked at
Amazon
Atlassian
LinkedIn
Shopify
Stripe
Amazon, Atlassian, LinkedIn, Shopify, Stripe

Problem statement

Implement a function camelCaseKeys that takes an object and returns a new object with all keys converted to camel case.

Camel case joins words together. The first word starts with a lowercase letter, and each following word starts with a capital letter.

Examples

camelCaseKeys({ "first_name": "Ada" });
// { firstName: "Ada" }

camelCaseKeys({
  "user-id": 1,
  "profile data": { "display_name": "Ada" }
});
// { userId: 1, profileData: { displayName: "Ada" } }

Requirements

  • Convert keys written in snake case, kebab case, space-separated case, or mixed separator case.
  • Return a new object. Do not mutate the original input.
  • Recursively convert keys inside nested plain objects.
  • Recursively handle objects inside arrays.
  • Keep primitive values, Date, RegExp, Map, Set, functions, and null unchanged.

Requirements & constraints

  • →Do not mutate the input object.
  • →Do not use external libraries.
  • →The function should support nested objects and arrays.
  • →Only plain object keys should be converted.

Examples

  • Example 1

    Input: { "first_name": "Ada", "last-name": "Lovelace" }

    Output: { firstName: "Ada", lastName: "Lovelace" }

  • Example 2

    Input: { "profile data": { "avatar_url": "/ada.png" } }

    Output: { profileData: { avatarUrl: "/ada.png" } }

How to approach Camel Case Keys

The strategy an interviewer expects you to reach for.

Camel-case keys rewrites object trees from API snake_case into JS-friendly keys without destroying arrays/primitives. Naive move Rename only the top level, or mutate the input object in place. Where it breaks Nested objects need recursion. Arrays should map elements, not become objects. Date/non-plain objects may need to pass through unchanged. The fix Split keys on non-alphanumeric separators, lowercase the first part, Capitalize the rest. Walk plain objects/arrays recursively into a new structure. Remember: New tree; recurse into plain objects/arrays; leave true primitives alone. Say this in the interview: I'd convert each key to camelCase, recurse through plain objects and arrays into a new structure, and avoid mutating the input.

Premium

The full solution is part of HelloFrontend Pro

The question above is free to read in full. Upgrade to unlock the interactive workspace and the senior-level walkthrough that go with it.

  • Runnable editor with the hidden test suite
  • Progressive hints that unlock as you get stuck
  • Senior-level reference solution with a line-by-line walkthrough
Unlock the full solution →

Already a member? Log in to open the workspace.

More JS Coding questions

View all JS Coding →
JS CodingMedium

4. Improve a function

JS CodingHard

Deep Clone II

JS CodingMedium

Get and Set by Path

JS CodingMedium

5. Throttle

JS CodingMedium

13. Deep Clone

JS CodingMedium

14. Event Emitter

Explore related prep hubs

  • All JavaScript coding
  • JavaScript interview questions
  • JS polyfill interview questions
  • Debounce & throttle questions
  • React interview hub
  • Amazon interview
  • Atlassian interview
  • LinkedIn interview
  • Shopify interview
  • Stripe interview