0%

Refresh Knowledge

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var pilots = [
{
id: 10,
name: "Poe Dameron",
years: 14,
},
{
id: 2,
name: "Temmin 'Snap' Wexley",
years: 30,
},
{
id: 41,
name: "Tallissan Lintra",
years: 16,
},
{
id: 99,
name: "Ello Asty",
years: 22,
}
];
1
const totalYears = pilots.reduce((acc, pilot) => acc + pilot.years, 0);

Get the most experienced pilot's flight years:

1
2
3
var mostExpPilot = pilots.reduce(function (oldest, pilot) {
return (oldest.years || 0) > pilot.years ? 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
2
3
4
5
6
7
8
const euros = [29.76, 41.85, 46.5];

const doubled = euros.reduce((total, amount) => {
total.push(amount * 2);
return total;
}, []);

doubled // [59.52, 83.7, 93]

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
2
3
4
5
6
7
8
const fruitBasket = ['banana', 'cherry', 'orange', 'apple', 'cherry', 'orange', 'apple', 'banana', 'cherry', 'orange', 'fig' ];

const count = fruitBasket.reduce( (tally, fruit) => {
tally[fruit] = (tally[fruit] || 0) + 1 ;
return tally;
} , {})

count // { banana: 2, cherry: 3, orange: 3, apple: 2, fig: 1 }
1
2
3
4
5
6
7
8
const colors = data.reduce((total, amount) => {
amount.c.forEach( color => {
total.push(color);
})
return total;
}, [])

colors //['blue','green','green','black','orange','blue','green','red']
1
2
3
4
5
6
7
8
9
10
const uniqueColors = data.reduce((total, amount) => {
amount.c.forEach( color => {
if (total.indexOf(color) === -1){
total.push(color);
}
});
return total;
}, []);

uniqueColors // [ 'blue', 'red', 'green', 'black', 'orange']

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…