Watch 10 video solutions for Undefined to Null, a medium level problem. This walkthrough by Web Dev Simplified has 73,519 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a deeply nested object or array obj, return the object obj with any undefined values replaced by null.
undefined values are handled differently than null values when objects are converted to a JSON string using JSON.stringify(). This function helps ensure serialized data is free of unexpected errors.
Example 1:
Input: obj = {"a": undefined, "b": 3}
Output: {"a": null, "b": 3}
Explanation: The value for obj.a has been changed from undefined to null
Example 2:
Input: obj = {"a": undefined, "b": ["a", undefined]}
Output: {"a": null,"b": ["a", null]}
Explanation: The values for obj.a and obj.b[1] have been changed from undefined to null
Constraints:
obj is a valid JSON object or array2 <= JSON.stringify(obj).length <= 105Problem Overview: You receive a JavaScript value that may contain nested objects or arrays. The task is to traverse the structure and replace every undefined value with null, while preserving the original structure and other values.
Approach 1: Recursive DFS Traversal (O(n) time, O(h) space)
The cleanest solution uses recursion to perform a depth-first traversal over the structure. Start by checking the type of the current value. If the value is exactly undefined, return null. If the value is an array, iterate through each index and recursively process each element. If the value is an object, iterate over its keys and recursively transform each property value.
The key insight is that arrays and objects are the only structures that can contain nested values requiring transformation. Primitive values like numbers, strings, and booleans can be returned directly. During traversal, every nested branch is visited exactly once, ensuring linear processing of the entire structure.
This approach naturally mirrors the hierarchical shape of JSON-like data. Each recursive call handles a smaller substructure until reaching primitives. Because recursion depth equals the nesting depth, auxiliary space depends on the maximum depth h. This pattern is closely related to techniques used in recursion and depth-first search for hierarchical data processing.
In practice, the algorithm works well for configuration objects, API responses, or parsed JSON-like structures where missing values must be normalized before serialization or storage.
Approach 2: Iterative Stack Traversal (O(n) time, O(n) space)
An iterative alternative replaces recursion with an explicit stack. Push the root value onto the stack and repeatedly process items until the stack is empty. When encountering arrays or objects, push their children onto the stack for later processing. When a value equals undefined, replace it with null.
This version performs the same logical traversal but avoids recursion depth limits. Each node is still processed once, giving O(n) time complexity. However, the stack may temporarily store many nodes, producing O(n) space usage in the worst case.
Iterative traversal is useful when the nesting depth could exceed the call stack limit or when environments discourage deep recursion.
Recommended for interviews: The recursive DFS approach is typically expected. It demonstrates clear understanding of object/array traversal and recursive decomposition. Mentioning the iterative stack alternative shows awareness of recursion limits and practical implementation trade‑offs.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive DFS Traversal | O(n) | O(h) | Best general solution for nested objects/arrays with manageable depth |
| Iterative Stack Traversal | O(n) | O(n) | When recursion depth could exceed call stack limits |