Get/Set Nested Property

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Get/Set Nested Property

Approach

Parse a dot-notation path (supporting bracket syntax for array indices) into an array of keys. For get, walk the object following each key and return the value or a default. For set, walk and auto-create missing intermediate objects, then assign the value at the final key.

Complexity Analysis

Time
O(k) where k is the path depth
Space
O(k) for the parsed key array

Pattern

Nested Property Access

Why It Works

Converting bracket notation to dot notation with a regex, then splitting on dots, normalizes all path styles into a uniform key array that can be traversed with a simple loop.

Updated Feb 2026