Skip to curriculum
Jump to the editor

3.7. Array.prototype.flat to any depth

Medium
Topics
15–30 min

Nested arrays turn up everywhere: pages of results, each a list; a menu tree's items; rows of cells. flat opens nested arrays into one list, as many levels deep as you ask. It is also the classic recursion exercise, with a few spec details that a quick version gets wrong: what counts as an array, what happens to holes, and what depth really means.

Write Array.prototype.myFlat(depth) in flat.js. It must behave like the built-in Array.prototype.flat for every case below.

Signature

JAVASCRIPT
Array.prototype.myFlat = function myFlat(depth) // returns a new array
  • depth: how many levels of nested arrays to open. When it is missing (or undefined) it is 1. Infinity opens every level.
  • Returns a new array. The original array and the arrays inside it are not changed.

Requirements

  1. With no depth, myFlat opens one level: arrays inside arrays stay as they are.
  2. With a depth, it opens that many levels; Infinity opens all of them, even 1,000 levels deep.
  3. depth is converted to a whole number: "2" is 2, 1.9 is 1, NaN and null are 0. A negative depth counts as 0. Only undefined means the default 1. A depth that is a BigInt or a Symbol, or an object whose valueOf returns one, throws a TypeError, as the built-in does ([[1]].flat(1n) throws).
  4. With depth 0, the result is a copy with nothing opened, but still without holes.
  5. Holes are removed from this and from every array that is opened; holes inside an array that is not opened stay. Empty arrays that are opened add nothing. undefined and null items are kept.
  6. Only real arrays are opened (as Array.isArray says). Array-like objects, strings and Sets are added as they are, and Symbol.isConcatSpreadable makes no difference.
  7. The order of the items is kept, and objects are the same objects, not copies.
  8. myFlat works on an array-like this, called with Array.prototype.myFlat.call(…), and reads its length once. length is converted to a whole number from 0 up ("2" and 2.9 are 2, -1 is 0); a BigInt or Symbol length throws a TypeError.
  9. It throws a TypeError when called on null or undefined.

Examples

JAVASCRIPT
[1, [2, 3], [4, [5]]].myFlat();          // [1, 2, 3, 4, [5]]
[1, [2, [3, [4]]]].myFlat(2);           // [1, 2, 3, [4]]
[1, [2, [3, [4]]]].myFlat(Infinity);    // [1, 2, 3, 4]
JAVASCRIPT
[1, , [2, , 3]].myFlat();                // [1, 2, 3]: holes removed
[[], [[]], 1].myFlat(Infinity);          // [1]
[1, [2]].myFlat(null);                   // [1, [2]]: null is depth 0
JAVASCRIPT
[{ 0: "a", length: 1 }, "bc"].myFlat();   // [{ 0: "a", length: 1 }, "bc"]: only arrays are opened

Environment

  • Plain JavaScript in a browser page; the tests call Array.prototype.myFlat from flat.js.
  • flat.js starts with "use strict". Keep it: in strict mode, this inside myFlat is exactly the value it was called on, so a call on null can be detected.
  • Not allowed: the built-in flat and flatMap. The tests delete them from Array.prototype while myFlat runs.
  • Out of scope: subclasses of Array (myFlat always returns a plain array), and arrays nested so deeply that recursion runs out of stack.

Hints

  1. When you open a nested array, how deep may you still open the arrays inside it?
  2. If a helper writes into one shared result array, what does it need to know about where to write next?

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…