Skip to curriculum
Jump to the editor

21.3. Print every key and value of a nested object

Easy
Topics
15–25 min

console.log of a large object shows a collapsed tree you have to click open, and JSON.stringify throws on the first cycle and drops undefined. When you debug a config, a store's state or an API response, it often helps to print every key and value as plain text, one per line, indented by depth.

Write printNested(value) in print-nested.js. It walks an object or array at every depth, builds one line per key, logs the whole text with a single console.log, and returns that text.

Signature

JAVASCRIPT
function printNested(value) // returns a string
  • value: usually an object or an array. It is never changed.
  • Returns the lines joined with "\n", and logs that same string once with console.log.

Requirements

  1. Each own, enumerable string key gets one line, in Object.keys order: key: value. Symbol keys, inherited keys and array holes are skipped.
  2. A key whose value is a non-empty plain object or array prints key: alone, and its keys follow, indented two more spaces.
  3. Arrays print their indexes as keys (0: …, 1: …).
  4. Strings print as JSON.stringify writes them: in double quotes, with quotes inside escaped.
  5. Every other value prints as String(value) does: 36, NaN, false, null, undefined, Symbol(id), 10 for 10n. Keys whose value is null or undefined are not skipped.
  6. An empty object prints key: {}; an empty array prints key: [].
  7. Only arrays and plain objects (prototype Object.prototype or null) are walked into. A Date prints its toISOString(), or Invalid Date. A Map, a Set or a class instance prints String(value).
  8. An object or array that the walk is already inside of prints key: [Circular]. An object that appears twice without being inside itself is printed in full both times.
  9. The top-level value follows the same rules: {} and [] print as {} and [], and a value that is not a container prints alone ("hi", 7).
  10. console.log is called once, with the returned text.
  11. The input is not changed.

Examples

JAVASCRIPT
printNested({ user: { name: "Ada", tags: ["math", "code"] }, active: true, notes: [] });
// user:
//   name: "Ada"
//   tags:
//     0: "math"
//     1: "code"
// active: true
// notes: []
JAVASCRIPT
const node = { id: 1, meta: { at: new Date(0) } };
node.meta.owner = node;
printNested(node);
// id: 1
// meta:
//   at: 1970-01-01T00:00:00.000Z
//   owner: [Circular]
JAVASCRIPT
printNested({}); // {}

Environment

  • Plain JavaScript in a browser page; the tests call the global printNested from print-nested.js and replace console.log while it runs.
  • Keys are printed as they are, without quotes.

Hints

  1. What does one call need to know besides the object, so that its lines start with the right number of spaces?
  2. Which objects should print as [Circular]: every object seen so far, or only the ones you are still inside of?

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…