-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-array-methods.js
More file actions
32 lines (26 loc) · 1.2 KB
/
Copy path6-array-methods.js
File metadata and controls
32 lines (26 loc) · 1.2 KB
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
const numbers = [5, 12, 8, 130, 44];
// The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
console.log(numbers.map((x) => x * 2));
// The find() method returns the value of the first element in the provided array that satisfies the provided testing function.
// If no values satisfy the testing function, undefined is returned.
console.log(numbers.find((x) => x > 10));
// The filter() method creates a new array with all elements that pass the test implemented by the provided function.
// If no values satisfy the testing function, empty array is returned.
console.log(numbers.filter((x) => x > 10));
// The reduce() method executes a reducer” callback function on each element of the array,
// passing in the return value from the calculation on the preceding element.
// The final result is a single value.
// Converts to string and concatenates all elements in the array
console.log(
numbers.reduce(
(previousValue, currentValue) => `${previousValue} ${currentValue},`,
""
)
);
// Sums all elements in the array
console.log(
numbers.reduce(
(previousValue, currentValue) => previousValue + currentValue,
0
)
);