Objects & Immutability

Set Nested Property by Path

mediumJS SpecificJS / TS3 tests

Problem Statement

Implement `setByPath(obj, path, value)` which sets the value at `path` in `obj`. If any intermediate properties do not exist, create them as objects or arrays based on the path keys.

Examples

Example 1:
const obj = {};
setByPath(obj, 'a.b.c', 42);
console.log(obj.a.b.c); // 42

Example 2:
const obj2 = {};
setByPath(obj2, 'users[0].name', 'Bob');
console.log(obj2.users[0].name); // 'Bob'
JavaScript & TypeScript starter code included
Run this as a live interview

Practice solo

3 test cases. No second person needed.

Get new interview exercises

Practice problems and review drills, sent when we publish new ones. No spam.

Unsubscribe anytime. We never share your address.