forked from lap00zza/Snippets.JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shorter_chain_in_array_map_filter.js
91 lines (81 loc) · 2.99 KB
/
shorter_chain_in_array_map_filter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const posts = [
{
"userId" : 1,
"id" : 1,
"title" : "sunt aut abc repellat provident occaecati excepturi optio reprehenderit",
"body" : "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit mol",
},
{
"userId" : 1,
"id" : 2,
"title" : "qui est esse",
"body" : "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\",
},
{
"userId" : 1,
"id" : 3,
"title" : "ea molestias abc quasi exercitationem repellat qui ipsa sit aut",
"body" : "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus t",
},
{
"userId" : 1,
"id" : 4,
"title" : "eum et est occaecati",
"body" : "ullam et saepe reiciendis voluptatem adipisci\nsit amet autem assumendavelit",
},
{
"userId" : 1,
"id" : 5,
"title" : "nesciunt abc quas odio",
"body" : "repudiandae veniam quaerat sunt sed\nalias aut fugiat sit autem sed est\nvoer neque",
},
{
"userId" : 1,
"id" : 6,
"title" : "dolorem eum magni eos aperiam quia",
"body" : "ut aspernatur corporis harum nihil quis provident sequi\nmollitia nobis tiae",
},
];
// Let say we want to get all posts with CAPITALIZE title and filter a word with `abc`
// Option 1, but these
var capitalizeTitle = posts.map(function(post) {
return post.title.toUpperCase()
})
var getOnlyABC = capitalizeTitle.filter(function(capsTitle) {
var abcRegex = /abc/ig; // we are interested in abc, /ig mean i = not case sensitive, g = global
return abcRegex.test(capsTitle) // this will check is there any abc in each title
})
// getOnlyAbc = [
// "SUNT AUT ABC REPELLAT PROVIDENT OCCAECATI EXCEPTURI OPTIO REPREHENDERIT",
// "EA MOLESTIAS ABC QUASI EXERCITATIONEM REPELLAT QUI IPSA SIT AUT",
// "NESCIUNT ABC QUAS ODIO"
// ]
// we can make this shorter with chaining
var getOnlyABC = posts.map(function(post) {
return post.title.toUpperCase()
}).filter(function(capsTitle) {
var abcRegex = /abc/ig;
return abcRegex.test(capsTitle)
})
// and we can make use of ES6 fat arrow.
const getOnlyABCES6 = posts
.map(post => post.title.toUpperCase())
.filter(capsTitle => {
const abcRegex = /abc/ig;
return abcRegex.test(capsTitle)
})
// But these method are using 2 method chaining with `.map` and `.filter`
// we can use `.reduce` to simplify this even further.
const getOnlyABCWithReduce = posts.reduce((accumulator, current) => {
const abcRegex = /abc/ig;
const capitalizeTitle = current.title.toUpperCase();
if(abcRegex.test(capitalizeTitle)) {
accumulator.push(capitalizeTitle)
}
return accumulator;
}, [])
// getOnlyABCWithReduce = [
// "SUNT AUT ABC REPELLAT PROVIDENT OCCAECATI EXCEPTURI OPTIO REPREHENDERIT",
// "EA MOLESTIAS ABC QUASI EXERCITATIONEM REPELLAT QUI IPSA SIT AUT",
// "NESCIUNT ABC QUAS ODIO"
// ]