Skip to curriculum
Jump to the editor

21.9. Remove falsy values at every depth

Easy
Topics
10–20 min

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.

Signature

JAVASCRIPT
function compactDeep(value) // returns the cleaned copy
  • value: any value. It is never changed.
  • Returns a new structure, or value itself when it is not an array or a plain object.

Requirements

  1. A value is removed when it is falsy: false, 0, -0, 0n, "", null, undefined and NaN.
  2. In an array, falsy items and holes are removed and the remaining items close up, in order (no gaps).
  3. In a plain object (prototype Object.prototype or null), keys whose values are falsy are removed; the other keys keep their order.
  4. Arrays and plain objects are cleaned at every depth, inside each other.
  5. 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.
  6. Other objects (Dates, including invalid ones, Maps, Sets, class instances) and functions are kept as they are, not walked into.
  7. The input is not changed, and the result shares no array or plain object with it.
  8. A top-level value that is not an array or a plain object is returned as it is, even if it is falsy.
  9. An own key named __proto__ (as JSON.parse creates) is kept as an ordinary key and changes no prototype.
  10. A value that contains itself throws a TypeError.

Examples

JAVASCRIPT
compactDeep([0, 1, false, 2, "", 3]); // [1, 2, 3]
JAVASCRIPT
compactDeep({
  name: "Ada",
  nickname: "",
  address: { street: null, city: "London" },
  tags: ["x", null, "y", undefined],
  settings: { beta: false },
});
// { name: "Ada", address: { city: "London" }, tags: ["x", "y"], settings: {} }
JAVASCRIPT
compactDeep(["0", "false", [], {}]); // ["0", "false", [], {}]
compactDeep(0);                      // 0

Environment

  • Plain JavaScript in a browser page; the tests call the global compactDeep from compact.js.
  • Only own, enumerable string keys are copied (Object.keys). Symbol keys are out of scope.

Hints

  1. Is there one expression that is false for exactly the eight falsy values, so you do not have to list them?
  2. When you remove an array item, what keeps the later items from leaving a gap?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…