Repeating is the best way to learn and remember things, especially when you have terrible long-term memory. Today I have read some good articles and also watched some videos about several high order functions' usage and know one thing called rxjs observable. Pretty cool stuff.
predicate function (filter): to test true or false – (boolean-valued function)
projection function (map): transform values and generate a new value
loop => for synchronised data
map/filter/reduce/ => asynchronized data & create a new array
.map() => get an array
So how does .map() work? Basically is takes 2 arguments, a callback and an optional context (will be considered as this in the callback) which I did not use in the previous example. The callback runs for each value in the array and returns each new value in the resulting array.
.reduce() =>
Just like .map(), .reduce() also runs a callback for each element of an array. What’s different here is that reduce passes the result of this callback (the accumulator) from one array element to the other.
The accumulator can be pretty much anything (integer, string, object, etc.) and must be instantiated or passed when calling .reduce().
1 | var pilots = [ |
1 | const totalYears = pilots.reduce((acc, pilot) => acc + pilot.years, 0); |
Get the most experienced pilot's flight years:
1 | var mostExpPilot = pilots.reduce(function (oldest, pilot) { |
accumulator => oldest. Callback compares the accumulator to each pilot. If a pilot has more years of experience than oldest, then that pilot becomes the new oldest so that’s the one I return.
1 | const euros = [29.76, 41.85, 46.5]; |
These operations are the map and filter methods rewritten as a reduce method.
For these examples, it would make more sense to use map or filter because they are simpler to use. The benefit of using reduce comes into play when you want to map and filter together and you have a lot of data to go over.
If you chain map and filter together you are doing the work twice. You filter every single value and then you map the remaining values. With reduce you can filter and then map in a single pass.
Use map and filter but when you start chaining lots of methods together you now know that it is faster to reduce the data instead.
1 | const fruitBasket = ['banana', 'cherry', 'orange', 'apple', 'cherry', 'orange', 'apple', 'banana', 'cherry', 'orange', 'fig' ]; |
1 | const colors = data.reduce((total, amount) => { |
1 | const uniqueColors = data.reduce((total, amount) => { |
Several ways to transfor nodeLists/HTMLCollection to Array
1.) var arr = Array.prototype.slice.call( htmlCollection ); This way is fastest actually, because of shallow copy
2.) var arr = [].slice.call(htmlCollection);
The slice() method returns an array. That returned array is a shallow copy of collection (NodeList) So it works faster than **Array.from()**
3.) var arr = Array.from(htmlCollection);
4.) var arr = […htmlCollection];
htmlcollection: contains blank text;
nodelist:
default method: length, entries, forEach, item, keys, values
调用entries方法会返回一个iterator(迭代器),关于iterator/iterable可以参见MDN,简单点说就是返回了一个可以遍历的对象,而这个对象实现了iterable protocal,所以需要用for…of遍历。
TO BE CONTINUED…