Skip to content

Commit 7d9682f

Browse files
committed
feat: add tutorial of arrays methods
1 parent 3f2b8f8 commit 7d9682f

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
<!-- <script src="src/proxy.js"></script> -->
2222
<!-- <script src="src/generator.js"></script> -->
2323
<!-- <script src="src/symbol.js"></script> -->
24+
<!-- <script src="src/arrays-methods.js"></script> -->
2425
</body>
2526
</html>

src/arrays-methods.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const array = [
2+
{ prop1: "One", prop2: 1 },
3+
{ prop1: "Two", prop2: 2 },
4+
{ prop1: "Three", prop2: 3 },
5+
{ prop1: "Four", prop2: 4 },
6+
{ prop1: "Five", prop2: 5 }
7+
]
8+
9+
// for (ES5)
10+
console.group("for");
11+
12+
for (let index = 0; index < array.length; index++) {
13+
const element = array[index];
14+
console.log(element);
15+
}
16+
17+
console.groupEnd();
18+
19+
// for..of (ES6)
20+
console.group("for..of");
21+
22+
for (const element of array) {
23+
console.log(element);
24+
}
25+
26+
console.groupEnd();
27+
28+
// forEach
29+
console.group("forEach");
30+
31+
array.forEach(function(element, index, pArr) {
32+
console.log(element);
33+
console.log(index);
34+
console.log(pArr);
35+
console.log("-----")
36+
})
37+
38+
array.forEach(element => {
39+
console.log(element);
40+
});
41+
42+
console.groupEnd();
43+
44+
// Map
45+
console.group("Map");
46+
47+
const newArray = array.map(element => {
48+
return element;
49+
});
50+
console.log(newArray);
51+
52+
const someArray = array.map(element => {
53+
return `Prop1: ${element.prop1}, prop2: ${element.prop2}`;
54+
});
55+
console.log(someArray);
56+
57+
console.groupEnd();
58+
59+
// Filter
60+
console.group("Filter");
61+
62+
const filteredArray = array.filter(element => element.prop2 >= 3);
63+
console.log(filteredArray);
64+
65+
console.groupEnd();
66+
67+
// Reduce
68+
console.group("Reduce");
69+
70+
const sumArray = array.reduce((total, element) => {
71+
return total + element.prop2;
72+
}, 0);
73+
console.log(sumArray);
74+
75+
console.groupEnd();
76+
77+
// Find
78+
console.group("Find");
79+
80+
const itemTwo = array.find(element => element.prop1 === "Two");
81+
console.log(itemTwo);
82+
83+
console.groupEnd();
84+
85+
// FindIndex
86+
console.group("FindIndex");
87+
88+
const itemFourIndex = array.findIndex(element => element.prop1 == "Four");
89+
console.log(itemFourIndex);
90+
91+
console.groupEnd();

0 commit comments

Comments
 (0)