-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheatSheet for Array methods JS.txt
More file actions
134 lines (77 loc) · 5.57 KB
/
CheatSheet for Array methods JS.txt
File metadata and controls
134 lines (77 loc) · 5.57 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
Array methods CheatSheet:
Method Name: forEach()
Example Update each element in an array by multiplying it by 2.
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(number, index, array) {
array[index] = number * 2;
});
console.log(numbers);
Does it mutate the source or not: The forEach() method does not mutate the source array. It only performs an operation on each element of the array without modifying it.
Argument List and Meaning:
callback (required): The function to execute on each element. It takes three arguments:
currentValue: The current element being processed in the array.
index (optional): The index of the current element being processed.
array (optional): The array that forEach() is being applied to.
What it returns: The forEach() method does not return anything. It performs an operation on each element of the array, but it does not produce a new array or a return value.
------------------------------------------------------------------------------------------------------------------------------------------
Method Name: map()
Example 1: Double each element in an array.
const numbers = [1, 2, 3, 4, 5];
const doubledNumbers = numbers.map(function(number) {
return number * 2;
});
console.log(doubledNumbers);
Does it mutate the source or not: The map() method does not mutate the source array. It creates a new array with the results of calling a provided function on each element of the original array.
Argument List and Meaning:
callback (required): The function to execute on each element. It takes three arguments:
currentValue: The current element being processed in the array.
index (optional): The index of the current element being processed.
array (optional): The array that map() is being applied to.
What it returns: The map() method returns a new array with the results of calling the provided function on each element of the original array.
Please note that the arrow function syntax can also be used instead of traditional function syntax in the examples.
------------------------------------------------------------------------------------------------------------------------------------------
Method Name: filter()
Example: Filter out even numbers from an array.
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const evenNumbers = numbers.filter(function(number) {
return number % 2 === 0;
});
console.log(evenNumbers);
Does it mutate the source or not: The filter() method does not mutate the source array. It creates a new array containing elements that pass the specified filtering condition.
Argument List and Meaning:
callback (required): The function to test each element of the array. It takes three arguments:
currentValue: The current element being processed in the array.
index (optional): The index of the current element being processed.
array (optional): The array that filter() is being applied to.
What it returns: The filter() method returns a new array with the elements that pass the filtering condition specified by the provided function.
Please note that the arrow function syntax can also be used instead of traditional function syntax in the example.
------------------------------------------------------------------------------------------------------------------------------------------
Method Name: reduce()
Example: Calculate the sum of all numbers in an array.
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce(function(accumulator, currentValue) {
return accumulator + currentValue;
}, 0);
console.log(sum);
Does it mutate the source or not: The reduce() method does not mutate the source array. It performs calculations on the elements of the array and returns a single value.
Argument List and Meaning:
callback (required): The function to execute on each element. It takes four arguments:
accumulator: The accumulated value computed by previous iterations or the initial value provided.
currentValue: The current element being processed in the array.
index (optional): The index of the current element being processed.
array (optional): The array that reduce() is being applied to.
initialValue (optional): The initial value for the accumulator. If not provided, the first element of the array is used as the initial value.
What it returns: The reduce() method returns a single value that results from the accumulation of values computed by the callback function.
------------------------------------------------------------------------------------------------------------------------------------------
Method Name: slice()
Example: Extract a portion of an array.
const fruits = ['apple', 'banana', 'orange', 'grape', 'mango'];
const slicedFruits = fruits.slice(1, 4);
console.log(slicedFruits);
Does it mutate the source or not: The slice() method does not mutate the source array. It returns a new array containing the extracted portion of the original array.
Argument List and Meaning:
start (optional): The index at which to begin extraction. If not specified, slice() starts from index 0.
end (optional): The index at which to end extraction. The extracted portion does not include the element at the end index. If not specified, slice() extracts all elements from the start index to the end of the array.
What it returns: The slice() method returns a new array containing the extracted portion of the original array.
Note: The original array is not modified. If either start or end is negative, it is treated as counting from the end of the array.
------------------------------------------------------------------------------------------------------------------------------------------