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.
Awesome series!! I was wondering if there is any difference between flatMap and map + concatAll.
Thks
No difference whatsoever. You nailed it.
concatAll could be:
Array.prototype.concatAll = function() { return this.reduce(function(a, b) { return a.concat(b); }, []); };
Right?
The JSbin needs to change matching "close.date.getMonth()" from 12 to 11, just like the last change in the video.
how we we incorporate flatMap into this example?
Yes. Array.prototype.concatAll = function() { return this.reduce((prev, curr) => prev.concat(curr)); };
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...
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.
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...
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.)
Yeah, I thought it was pretty odd as well. For what it's worth, he has a pretty in-depth course on Front End Masters, though it's more focused on RxJS.
concatAll can also be
Array.prototype.concatAll = function() {
return [].concat(...this);
};
I just had to stop by and say that this series is VERY well done. Your ability to convey these concepts in a short concise and easy to grasp way is excellent. Nicely done.