-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayMap-forEach.js
More file actions
40 lines (25 loc) · 1.08 KB
/
ArrayMap-forEach.js
File metadata and controls
40 lines (25 loc) · 1.08 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
// .map() and forEach() functions
// 1) forEach
// Takes a callback function as an argument and passes the callback function each value of the array, its index, and the array itself. Does not return anything
const myArray = ['one', 'two', 'three', 'four', 'five']
function callbackFunction(value, index, array) {
console.log(value, index, array)
// array[index] = value + 's'
}
myArray.forEach(callbackFunction)
console.log(myArray)
myArray.forEach((value, index, array) => {
console.log(value, index, array)
array[index] = value + 's'
})
// 2) map()
// Takes a callback function as an argument and passes the callback function each value of the array, its index, and the array itself. And returns a new array of whatever is returned in the callback function
const myArray2 = ['one', 'two', 'three', 'four', 'five']
function callbackFunction2(value, index, array) {
console.log(value, index, array)
return value +'s'
}
const newArray = myArray2.map(callbackFunction2)
console.log(newArray)
const newArray2 = myArray.map((value) => value + 's')
console.log(newArray2)