JS: Trees
2026-02-21 03:00 Diff

Full access to materials

Sign up and get access to this and dozens of other courses

Sign up

Let's practice one more data aggregation technique on file systems. Let's write a function that takes a directory as input and returns a list of directories on the first level of nesting and the number of files inside each of them, including all subdirectories.

This task breaks down into two smaller ones:

  • Implementing the function counting files in a directory
  • Calling this function for each of the subdirectories

Let's start by counting the number of files. This is a classic aggregation task:

The next step is to extract all the children from the initial node and apply a count to each of them:

I.e., we accessed the children directly by first filtering them, and mapped them to the desired array, which contained the name and number of files for each directory.

Recommended programs