809. Undefined to Null
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":true,"b":[1,2,undefined]}
Output : { "a": true, "b": [1, 2, null] }
Explanation :
The value for object obj.b[2] has been changes from undefined null.
Example 2:
Input : obj = { "a": undefined, "b": undefined }
Output : { "a": null, "b": null }
Explanation :
The value for object obj.b[2] has been changes from undefined null.
Now Your Turn!
Pick the correct output for the given inputInput : obj = { "a": ["1", 2, undefined], "b": [1, 2, undefined] }
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- obj is a valid JSON object or array
- 2 <= JSON.stringify(obj).length <= 105