In addition to flat Arrays, programmers must often deal with nested Arrays. For example let's say we have an Array of stock exchanges, each of which is represented by an array of all the stocks listed on that exchange. If we were looking for a stock that matched a certain criteria, we would first need to loop through all of the exchanges, and then all of the stocks within.
In these situations, most developers would nest two loops. However in this lesson we will write a new Array function "concatAll" which will automatically flatten nested arrays buy one dimension. This will remove the need to ever use a nested loop to flatten a nested array.
Thus far we've learned about three of the arrays methods, for each, map, and filter. In the previous example we learned that we could chain map and filter together to transform arrays into new arrays. Then we can use the for each method to consume all the data in the newly created transformed away and then do something with it, like print it out to the console.
Working with arrays is common, but even more common in programming is working with nested data structures like arrays of arrays, for example. Let's take a look at how we can use functions to flatten these nested data structures so that we can then further transform them.
An example of a nested data structure is the entire stock market. The entire stock market is actually made up of several exchanges. Each of these exchanges has multiple stocks. I've represented this here as a nested data structure, an array of arrays.
The first array is stock records for the Dow Industrial today. The next array is stock records for the NASDAQ. Let's imagine that we want to print out all of the stocks in all of the exchanges. One thing we can do is nest for each.
I'm going to call the for each method on exchanges, pass in a function that accepts the exchange. Then I'm going to for each over all of the stocks in each exchange. Finally I'm going to print each stock to the screen.
If everything went well we should see all of the stock records printed out to the console. Sure enough we see all the stocks from all of the exchanges. It turns out that we can take this very common pattern and we can abstract it into a useful function.
This function is called concat all. Unlike map, filter, and for each, it doesn't already exist on the array, so we're going to add it. The concat all function is very, very simple. It doesn't even accept any arguments. You run it on a nested array, and it creates a new array that contains the flattened resulted of that nested array.
Let's take a look at how we would execute this. We're going to follow the exact same pattern that we had in the example below. We're going to take this, which in this context is the outer array, and we're going to run for each on it. Each one of those items is expected to be another array, a subarray if you will.
Then we're going to call for each on the subarray. For every item in there we're going to add that item to a new collection of results. Finally concat all returns the results. Notice that concat all only flattens a nested array by one dimension.
If we were to run concat all on a three dimensional array, it would output a two dimensional array. On a four dimensional array a three dimensional array, a two dimensional array a one dimensional array, and so on.
Notice also that concat all will not work on a flat array because it assumes that each item in the array is another array. Concat all can only be used on arrays of two or more dimensions. Now I can take the code below and replace it with concat all, call for each, get all the stocks, and format this slightly differently.
I've created a new array containing only the stocks in all the exchanges. Now I'm going to for each over it, and I'm going to log the stock to the console. I'll comment out this code here, and we should see that when we run this we get the identical result.