Skip to content

Commit 20adaed

Browse files
committed
map(), filter() & reduce() method implement
1 parent 6030a5b commit 20adaed

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Chapter 5/20_map_filter_reduce.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
let arr = [45, 23, 21]
2+
// Array map method
3+
let a = arr.map((value, index, array) => {
4+
// console.log(value, index, array)
5+
return value + index
6+
})
7+
// console.log(arr)
8+
9+
// Array filter method
10+
let arr2 = [45, 23, 21, 0, 3, 5]
11+
let a2 = arr2.filter((a) => {
12+
return a < 10
13+
})
14+
// console.log(a2, arr2)
15+
16+
// Array reduce method
17+
let arr3 = [1, 2, 3, 5, 2, 1]
18+
const reduce_func = (h1, h2) => {
19+
return h1 + h2
20+
}
21+
let newarr3 = arr3.reduce(reduce_func)
22+
console.log(newarr3)

0 commit comments

Comments
 (0)