Data built from forms and partial updates fills up with empty slots: null for fields the user skipped, "" for cleared inputs, undefined where an optional value was never set, false from unchecked boxes. Before such data is stored, logged or shown as a summary, it is often cleaned: every falsy value removed, at every depth.
Write compactDeep(value) in compact.js. It returns a cleaned copy: falsy items are removed from arrays, and keys whose values are falsy are removed from objects, at every level of nesting.
A value is removed when it is falsy: false, 0, -0, 0n, "", null, undefined and NaN.
In an array, falsy items and holes are removed and the remaining items close up, in order (no gaps).
In a plain object (prototype Object.prototype or null), keys whose values are falsy are removed; the other keys keep their order.
Arrays and plain objects are cleaned at every depth, inside each other.
Truthy values that look empty are kept: "0", "false", " ", -1, Infinity, and every object or array, including one that is empty, or becomes empty after cleaning.
Other objects (Dates, including invalid ones, Maps, Sets, class instances) and functions are kept as they are, not walked into.
The input is not changed, and the result shares no array or plain object with it.
A top-level value that is not an array or a plain object is returned as it is, even if it is falsy.
An own key named __proto__ (as JSON.parse creates) is kept as an ordinary key and changes no prototype.