-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-methods.txt
More file actions
118 lines (90 loc) · 2.71 KB
/
Copy patharray-methods.txt
File metadata and controls
118 lines (90 loc) · 2.71 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const arr = [1, 3, 5, 7];
const users = [
{ name: 'grace', city: 'Manchester', age: 30 },
{ name: 'monica', city: 'Liverpool', age: 24 },
];
------------------------------------------------------------------------------------
keys() - Example
const iterator = arr.keys();
for (const key of iterator) {
console.log(key);
}
Result -
0
1
2
3
------------------------------------------------------------------------------------
values() - Example
const iterator = arr.values();
for (const value of iterator) {
console.log(value);
}
Result -
1
3
5
7
------------------------------------------------------------------------------------
push() - Example
console.log(arr.push(9));
console.log(arr);
Result - 5 - new length of the array
[1, 3, 5, 7, 9]
------------------------------------------------------------------------------------
pop() - Example
console.log(arr.pop());
console.log(arr);
Result - 7 // last element of the array
[1, 3, 5]
------------------------------------------------------------------------------------
slice() - Example
console.log(arr.slice(1, 3));
console.log(arr);
// remain from index 1 to 2 (last one is not included)
Result - 6 // new length of the array
[2, 4, 1, 3, 5, 7]
------------------------------------------------------------------------------------
splice() - Example
arr.splice(1, 0, 2); // insert ‘2’ at index 1
console.log(arr);
Result - [1, 2, 3, 5, 7]
Example 2 -
arr.splice(3, 1, 4); // replace 1 item at index 3
console.log(arr);
Result 2 - [1, 3, 5, 4]
------------------------------------------------------------------------------------
Reduce() - Example
console.log(arr.reduce((pre, curr) => pre + curr));
Result - 16
------------------------------------------------------------------------------------
find() - Example
console.log(arr.find((num) => num > 3));
Result - 5
------------------------------------------------------------------------------------
filter() - Example
console.log(arr.filter((num) => num > 3));
const newUsers = users.filter((user) => user.age > 25);
console.log(newUsers);
Result - [5, 7]
[ { name: 'grace', city: 'Manchester', age: 30 } ]
------------------------------------------------------------------------------------
map() - Example
arr.map((item) => item * 2)
users.map((user) => console.log(user.name));
Result - [2, 6, 10, 14]
grace
monica
------------------------------------------------------------------------------------
forEach() - Example
arr.forEach((item) => console.log(item * 2));
Result -
2
6
10
14
------------------------------------------------------------------------------------
includes() - Example
console.log(arr.includes(3));
Result - True
------------------------------------------------------------------------------------