-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharrays-methods.js
More file actions
91 lines (65 loc) · 1.61 KB
/
arrays-methods.js
File metadata and controls
91 lines (65 loc) · 1.61 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
const array = [
{ prop1: "One", prop2: 1 },
{ prop1: "Two", prop2: 2 },
{ prop1: "Three", prop2: 3 },
{ prop1: "Four", prop2: 4 },
{ prop1: "Five", prop2: 5 }
]
// for (ES5)
console.group("for");
for (let index = 0; index < array.length; index++) {
const element = array[index];
console.log(element);
}
console.groupEnd();
// for..of (ES6)
console.group("for..of");
for (const element of array) {
console.log(element);
}
console.groupEnd();
// forEach
console.group("forEach");
array.forEach(function(element, index, pArr) {
console.log(element);
console.log(index);
console.log(pArr);
console.log("-----")
})
array.forEach(element => {
console.log(element);
});
console.groupEnd();
// Map
console.group("Map");
const newArray = array.map(element => {
return element;
});
console.log(newArray);
const someArray = array.map(element => {
return `Prop1: ${element.prop1}, prop2: ${element.prop2}`;
});
console.log(someArray);
console.groupEnd();
// Filter
console.group("Filter");
const filteredArray = array.filter(element => element.prop2 >= 3);
console.log(filteredArray);
console.groupEnd();
// Reduce
console.group("Reduce");
const sumArray = array.reduce((total, element) => {
return total + element.prop2;
}, 0);
console.log(sumArray);
console.groupEnd();
// Find
console.group("Find");
const itemTwo = array.find(element => element.prop1 === "Two");
console.log(itemTwo);
console.groupEnd();
// FindIndex
console.group("FindIndex");
const itemFourIndex = array.findIndex(element => element.prop1 == "Four");
console.log(itemFourIndex);
console.groupEnd();