Problem, as an interviewer would state it:
"Implement clearNullish(value) — recursively removing every null and undefined from an object or array, without touching other falsy values, without mutating the input, and without crashing on a circular reference."
Examples:
clearNullish({ a: 1, b: null, c: { d: undefined, e: 0 } });
// { a: 1, c: { e: 0 } }
Clarifying questions expected:
- Should
0,""andfalsesurvive? (They are falsy but not nullish.) - For arrays, should nullish elements be removed and the rest shifted down, or should surviving elements keep their original indices?
- What should happen with a circular reference, and with non-plain objects such as a
Date?
Code / implementation expected: Yes — real, direct proof on nested data, on a circular structure, and in both array modes.