Skip to content

Commit 40994d7

Browse files
committed
Add reduce
1 parent ecc6acf commit 40994d7

File tree

3 files changed

+69
-20
lines changed

3 files changed

+69
-20
lines changed

README.md

+49-20
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,12 @@
22

33
Code snippets with gifs, to avoid copy and paste the code and write it all.
44

5-
## Files for each snippet
6-
7-
There is a file for each functionality, to be able to execute it.
8-
9-
Example:
10-
11-
- File es6/forEach/forEach.js
12-
13-
- Make it executable:
14-
15-
```
16-
chmod +x es6/forEach/forEach.js
17-
```
18-
19-
- Run it:
20-
21-
```
22-
./es6/forEach/forEach.js
23-
```
24-
255
## Table of Contents
266

277
- [forEach](#forEach)
288

9+
- [reduce](#reduce)
10+
2911
## forEach
3012

3113
> Example:
@@ -41,3 +23,50 @@ array.forEach((item, index) => {
4123
> Gif:
4224
4325
![Escribir forEach paso a paso](es6/forEach/forEach.gif?raw=true)
26+
27+
## reduce
28+
29+
> Example:
30+
31+
```javascript
32+
var pilots = [
33+
{
34+
id: 10,
35+
name: "Poe Dameron",
36+
years: 14,
37+
},
38+
{
39+
id: 41,
40+
name: "Tallissan Lintra",
41+
years: 16,
42+
},
43+
];
44+
45+
const totalYears = pilots.reduce((acc, pilot) => acc + pilot.years, 0);
46+
```
47+
48+
> Gif:
49+
50+
![Escribir forEach paso a paso](es6/reduce/reduce.gif?raw=true)
51+
52+
> [Useful use cases](https://itnext.io/useful-reduce-use-cases-91a86ee10bcd).
53+
54+
## Files for each snippet
55+
56+
There is a file for each functionality, to be able to execute it.
57+
58+
Example:
59+
60+
- File es6/forEach/forEach.js
61+
62+
- Make it executable:
63+
64+
```
65+
chmod +x es6/forEach/forEach.js
66+
```
67+
68+
- Run it:
69+
70+
```
71+
./es6/forEach/forEach.js
72+
```

es6/reduce/reduce.gif

2.17 MB
Loading

es6/reduce/reduce.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env node
2+
3+
// From https://medium.com/poka-techblog/simplify-your-javascript-use-map-reduce-and-filter-bd02c593cc2d
4+
5+
var pilots = [
6+
{
7+
id: 10,
8+
name: "Poe Dameron",
9+
years: 14,
10+
},
11+
{
12+
id: 41,
13+
name: "Tallissan Lintra",
14+
years: 16,
15+
},
16+
];
17+
18+
const totalYears = pilots.reduce((acc, pilot) => acc + pilot.years, 0);
19+
console.log({ totalYears });
20+

0 commit comments

Comments
 (0)