• 0

Implement concatAll in javascript

Querying Nested Arrays

To work with tree data structure we need to flatten them.

  • We will be solving a problem using Array.prototype.forEach().
  • We will define Array.prototype.concatAll() using Array.prototype.forEach().
  • We will solve the same problem using Array.prototype.concatAll()

This post is a follow up post of the
- Getting started Functional Programming in JavaScript. I would recommend reading that post first.

Problem

Flatten the movieLists array into an array of video ids

Let's solve with Array.prototype.forEach()


Flattening trees (2-D) arrays with nested forEach is relatively straight forward as we add each item explicitly. But this is the behavior which can easily be abstracted.


Implement Array.prototype.concaltAll() using Array.prototype.forEach().
Use map() and concatAll() to project and flatten the movieLists into an array of video ids.


Further Reading