The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Advanced Flattening

Advanced Flattening

11:58
In this lesson we solidify our understanding of how to flatten collections. This is perhaps the most important skill when learning to program without loops. We will try our hand at flattening not just a two dimensional collection, but a three-dimensional collection. Later on it will become clear how these skills relate to asynchronous programming.
Watch this lesson now
Avatar
egghead.io

In this lesson we solidify our understanding of how to flatten collections. This is perhaps the most important skill when learning to program without loops. We will try our hand at flattening not just a two dimensional collection, but a three-dimensional collection. Later on it will become clear how these skills relate to asynchronous programming.

Avatar
kevin

Running the final JSBin script included in the lesson does not render the results expected. Are there browser requirements or library dependencies that I'm not understanding?

In reply to egghead.io
Avatar
kevin

Nevermind – it appears as though the month is still set to 12 and not a zero-based month. Changing it to 11 fixed it.

In reply to kevin
Avatar
José

Awesome series!!
I was wondering if there is any difference between flatMap and map + concatAll.

Thks

Avatar
Jafar

No difference whatsoever. You nailed it.

In reply to José
Avatar
Eduardo

concatAll could be:

Array.prototype.concatAll = function() {
return this.reduce(function(a, b) {
return a.concat(b);
}, []);
};

Right?

Avatar
Tudor-Cristian

The JSbin needs to change matching "close.date.getMonth()" from 12 to 11, just like the last change in the video.

Avatar
Zach

how we we incorporate flatMap into this example?

In reply to Jafar
Avatar
Zhenyang Hua

Yes.
Array.prototype.concatAll = function() {
return this.reduce((prev, curr) => prev.concat(curr));
};

In reply to Eduardo
Avatar
Marcel

The part on advanced flattening, I found very good. I was using observables, my program did not work. I thought: back to the course on observables. And there was the answer one to one because the course anticipated on my error: programmers that want to access array element [0] within an observable. Like me.

But still I do not understand why.

If the asynchronous call to the database always returns a complete object. In case of the example on the stock market: the returned object has an array of exchanges that each have an array of stocks that each have an array of closes. And the object, once received, is complete through out all the levels. Like in the example and, in my case all objects that are returned from the database.

So in this example: why can I not apply synchronous programming within that object once received. And directly access exchanges[0].stocks[0].closes[0] if I want to.
In Angular2, when I use interpoltion, so {{exchanges[0].stocks[0].closes[0]}}, I get error

TypeError: Cannot read property '0' of undefined in [ etcetera...
Avatar
Rafael

I'm also learning this but my understanding is that at the moment you 'forEach' an observable, you don't have anything to work with yet. At that point you simply describe what to do with the data when it arrives, and this happens step by step, map by map, forEach by forEach.

To do what you want, you would need an Array with that structure, something that exists completely at the time you're calling ie.: exchanges[0].stocks[0]... with observables, each step/map level kinda depends on each other.

In reply to Marcel
Avatar
Charles

So what's the point of having a map function (that adds +1 dimension), if at the end of the day what we really want is a flattened array?
Shouldn't be map implemented as flatMap, and map renamed to unflattedMap? just saying...

In reply to Jafar
Avatar
Zac Smith

Jafar refers several times to future lessons where we will apply these principles to asynchronous scenarios. But the course ends at "Advanced Flattening". Am I missing something?

(Great course so far, by the way.)

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?