You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Iterate through the elements of the array and execute callback for each element
20
+
- Callback parameters:
21
+
- 1st parameter: Element
22
+
- 2nd parameter: Index
23
+
- Does not return anything (undefined)
24
+
*/
25
+
26
+
// Example 1: Simple iteration
27
+
// const arr = [100, 150, 200, 250, 300]
28
+
// arr.forEach((element, index) => {
29
+
// console.log(index, element)
30
+
// })
31
+
32
+
/* --------------- */
33
+
// Example 2: Calculate sum of elements of an array
34
+
// const arr = [129, 346666, 2044034, 1238042]
35
+
// let sum = 0
36
+
// arr.forEach((element, index) => {
37
+
// sum += element
38
+
// })
39
+
// console.log(sum)
40
+
41
+
/* --------------- */
42
+
// Example 3: Find the largest number in the array
43
+
// const arr = [250, 150, 600, 450, 300]
44
+
// let max = 0
45
+
// arr.forEach((element) => {
46
+
// if(element > max) {
47
+
// max = element
48
+
// }
49
+
// })
50
+
// console.log(max)
51
+
52
+
/* --------------- */
53
+
// Example 4: Filter even numbers from an array
54
+
// const arr = [100, 97, 86, 91, 75]
55
+
56
+
// const result = []
57
+
// arr.forEach((element) => {
58
+
// if(element % 2 == 0) {
59
+
// result.push(element)
60
+
// }
61
+
// })
62
+
// console.log(result)
63
+
64
+
/* --------------- */
65
+
// Example 5: Return an array which has elements multipled by 2
66
+
// Expected O/P: [4, 6, 10, 14, 18]
67
+
// const arr = [2, 3, 5, 7, 9]
68
+
69
+
// const result = []
70
+
// arr.forEach(element => {
71
+
// result.push(element * 2)
72
+
// })
73
+
// console.log(result)
74
+
75
+
/*
76
+
2. filter() ***
77
+
- Syntax:
78
+
arr.filter((element, index) => {
79
+
return "condition"
80
+
})
81
+
- Iterate through the elements of the array and execute callback for each element
82
+
- Callback parameters:
83
+
- 1st parameter: Element
84
+
- 2nd parameter: Index
85
+
- Returns a filtered array based on the condition provided. For each element, a boolean value is returned by the callback, if true - value is added to the filtered array, else skipped
86
+
- Does not modify the original array
87
+
- Additional pointers
88
+
- If "true" boolean value is returned by the callback, filtered array will have all the elements
89
+
- If "false" boolean value is returned by the callback, filtered array will be an empty array
90
+
- filteredArray.length <= originalArray.length
91
+
*/
92
+
93
+
/* --------------- */
94
+
// Example 1: Filter even numbers from an array
95
+
// const arr = [100, 97, 86, 91, 75]
96
+
97
+
// const result = arr.filter(element => {
98
+
// return element % 2 == 0
99
+
// })
100
+
// console.log(result)
101
+
102
+
/* --------------- */
103
+
// Example 2: Filter student names which have exactly 5 characters
104
+
// const students = ['Kelly', 'Kayla', 'Jeff', 'Madeline', 'Sonal']
105
+
// const result = students.filter(student => {
106
+
// return student.length == 5
107
+
// })
108
+
// console.log(result)
109
+
110
+
// const arr = [3, 6, 2, 5, 1]
111
+
// const result = arr.filter(e => {
112
+
// return false
113
+
// })
114
+
// console.log(result)
115
+
116
+
/*
117
+
3. map() ***
118
+
- Syntax:
119
+
arr.map((element, index) => {
120
+
return [modifiedElement]
121
+
})
122
+
- Iterate through the elements of the array and execute callback for each element
123
+
- Callback parameters:
124
+
- 1st parameter: Element
125
+
- 2nd parameter: Index
126
+
- Returns a modified array based on return value provided. For each element, a modified value can be returned which will be the element at that index in the returned array
127
+
- Does not modify the original array
128
+
- Additional pointers
129
+
- mappedArray.length = originalArray.length
130
+
- If nothing is returned, undefined is the value at that index
131
+
*/
132
+
133
+
/* --------------- */
134
+
// Example 1: Return an array which has elements multipled by 2
135
+
// Expected O/P: [4, 6, 10, 14, 18]
136
+
// const arr = [2, 3, 5, 7, 9]
137
+
// const result = arr.map(element => {
138
+
// return element * 2
139
+
// })
140
+
// console.log(result)
141
+
142
+
/* --------------- */
143
+
// Example 2: Return an array with square of elements in the input array
144
+
// Expected O/P: [4, 49, 9, 36, 25]
145
+
// const arr = [2, 7, 3, 6, 5]
146
+
// const result = arr.map(e => {
147
+
// return e ** 2
148
+
// })
149
+
// console.log(result)
150
+
151
+
/* --------------- */
152
+
// Example 3: Return an array with full name of users
0 commit comments