-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path24.js
executable file
·42 lines (25 loc) · 950 Bytes
/
24.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
// Array FlatMap ()
// The flatMap() method first maps each element of an
// array using a mapping function,
// then flattens it into a new array.
const arr = [1, 2, 3, 4, 5];
let search = arr.flatMap((e) => e ** 2);
console.log(search);
/*
flatMap() Return Value
Returns a new array after mapping every element using callback
Notes:
The flatMap() method does not change the original array.
The flatMap() method is equivalent to array.map().flat().
*/
// defining an array
let numbers = [1, 2, 3, 4, 5];
// incrementing each element of array using map()
let afterMapping = numbers.map((element) => element + 2);
// flattening the array using flat()
console.log(afterMapping);
let afterFlattening = afterMapping.flat();
console.log(afterFlattening); // [ 3, 4, 5, 6, 7 ]
// using flatMap() instead of flat() and map()
let after_flatMap = numbers.flatMap((element) => element + 2);
console.log(after_flatMap); // [ 3, 4, 5, 6, 7 ]