-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathJavaScriptArrayMethods.html
83 lines (67 loc) · 2.23 KB
/
JavaScriptArrayMethods.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JS Array Methods</title>
<script>
// forEach, filter, map -> Don't manipulate the original array
/* ----------------------------------------------------------------
1. forEach
- Iterate and run the callback for each element
- Cannot manipulate any elements inside the callback function for the original array
- Does not return any value
Syntax:
arr.forEach(function(element){
})
*/
// let arr = [3, 1, 5, 2, 6]
// for (element of arr) {
// console.log(element)
// }
// for (index in arr) {
// console.log(index)
// }
// arr.forEach(function(element) {
// console.log(element)
// })
/* ----------------------------------------------------------------
2. filter
- Iterate and run the callback for each element
- Cannot manipulate any elements inside the callback function for the original array
- A condition needs to be returned inside the callback
- Filter menthod returns a new array with all filtered elements which satisfy the given condition
[Size of returned array need not be same as original array]
Syntax:
arr.filter(function(element){
return [condition]
})
*/
// let arr = [3, 1, 5, 2, 6]
// let filteredArray = arr.filter(function(element){
// return element % 2 === 1
// })
// console.log(filteredArray)
/* ----------------------------------------------------------------
3. map
- Iterate and run the callback for each element
- Cannot manipulate any elements inside the callback function for the original array
- Inside the callback function, return updated element for each iteration
- Map method returns a new array with all updated elements which were returned for each iteration
[Size of returned array is exactly same as original array]
Syntax:
arr.map(function(element){
return [updated element]
})
*/
let arr = [3, 1, 5, 2, 6]
// let updatedArray = arr.map(function(element) {
// return element + 5
// })
console.log(updatedArray)
</script>
</head>
<body>
</body>
</html>