Undefined to Null - Problem

When working with JavaScript data structures, you'll often encounter undefined values that can cause issues during JSON serialization. Your task is to create a function that transforms any deeply nested object or array by replacing all undefined values with null.

Why does this matter? When you call JSON.stringify() on an object containing undefined values, those properties are completely omitted from the resulting JSON string. However, null values are preserved. This function ensures your serialized data maintains its structure and doesn't lose important properties.

Example: JSON.stringify({a: undefined, b: 5}) returns "{"b":5}", but JSON.stringify({a: null, b: 5}) returns "{"a":null,"b":5}".

The input can be arbitrarily nested with objects, arrays, primitive values, and undefined values at any level.

Input & Output

example_1.js โ€” Basic Object
$ Input: {"a": undefined, "b": 5, "c": undefined}
โ€บ Output: {"a": null, "b": 5, "c": null}
๐Ÿ’ก Note: All undefined values at the root level are replaced with null, while other values remain unchanged
example_2.js โ€” Nested Array
$ Input: [1, undefined, [undefined, "hello", undefined], {"x": undefined, "y": 42}]
โ€บ Output: [1, null, [null, "hello", null], {"x": null, "y": 42}]
๐Ÿ’ก Note: Nested undefined values in arrays and objects are recursively replaced with null at all levels
example_3.js โ€” Edge Cases
$ Input: {"nested": {"deep": {"value": undefined}}, "array": [undefined, null, 0, false]}
โ€บ Output: {"nested": {"deep": {"value": null}}, "array": [null, null, 0, false]}
๐Ÿ’ก Note: Deeply nested undefined values are found and replaced. Note that existing null, 0, and false values remain unchanged

Constraints

  • The input obj can be any valid JavaScript value
  • Maximum nesting depth is 1000 levels
  • Objects can have up to 104 properties
  • Arrays can have up to 104 elements
  • Only undefined values should be replaced, not other falsy values

Visualization

Tap to expand
Undefined to Null TransformationBefore (with undefined)undefined5"text"undefinedNested: [undefined, 42]TransformAfter (undefined โ†’ null)null5"text"nullNested: [null, 42]Single-Pass Process1Visit node2Check undefined3Replace with null4ContinueTime: O(n) - visit each value once | Space: O(d) - recursion depth
Understanding the Visualization
1
Start at Root
Begin at the top level of the data structure
2
Examine Each Value
Check if the current value is undefined
3
Replace if Undefined
If undefined found, immediately replace with null
4
Recurse into Nested
For objects/arrays, recursively process all nested values
5
Complete in One Pass
Each value is processed exactly once
Key Takeaway
๐ŸŽฏ Key Insight: By processing each value exactly once during traversal, we achieve optimal O(n) time complexity while maintaining clean, readable recursive code.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28 Apple 22
34.2K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen