-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18_ArrayMethods.js
204 lines (155 loc) · 5.79 KB
/
18_ArrayMethods.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*----------------------------------------------------------------------------------------------*/
/* Common array methods
------------------------------------------------------------------------------------------------*/
const alpha = ["A", "B", "C", "D", "E", "F", "G"]
/*--------------------------------------------------------*/
/* Push and Pop
//
// Add and Remove items in the array
// MUTATE ORIGINAL ARRAY!
----------------------------------------------------------*/
alpha.push("H", "I")
alpha.pop("H")
/*--------------------------------------------------------*/
/* Slice
//
// Returns selected elements in a new array
// DOES NOT MUTATE ORIGINAL ARRAY
----------------------------------------------------------*/
// Returns all elements starting from 2
// console.log(alpha.slice(2));
// Last index is not included
// console.log(alpha.slice(3, 6));
// Returns last elements of the array
// console.log(alpha.slice(-2));
/*--------------------------------------------------------*/
/* Splice
//
// Returns selected elements in a new array
// MUTATES ORIGINAL ARRAY!
----------------------------------------------------------*/
// Deletes and Returns all elements starting from index 2
// const res = alpha.splice(2)
// console.log(alpha, res);
// Deletes and Returns 3 elements starting from index 1
// const res2 = alpha.splice(1, 3)
// console.log(alpha, res2);
// Deletes and Returns 3 elements starting from index 1 and insert "Yee" instead
// const res3 = alpha.splice(1, 3, "Yee")
// console.log(alpha, res3);
/*--------------------------------------------------------*/
/* REVERSE
//
// Returns elements in a reversed order
// MUTATES ORIGINAL ARRAY!
----------------------------------------------------------*/
// const res = alpha.reverse()
// console.log(alpha, res);
/*--------------------------------------------------------*/
/* JOIN
//
// Joins array elements into a string
// DOES NOT MUTATE ORIGINAL ARRAY
----------------------------------------------------------*/
// Accepts separators as a parameter (default is a comma)
// const bond = alpha.join("")
// console.log(bond);
/*--------------------------------------------------------*/
/* Includes
//
// Returns true if array contains a specific value and false if doesn't
// DOES NOT MUTATE ORIGINAL ARRAY
----------------------------------------------------------*/
const inc = alpha.includes("A")
// console.log(inc);
/*----------------------------------------------------------------------------------------------*/
/* Callback array methods
------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------*/
/* Filter
//
// Returns a new array with filtered elements
// Accepts callback function that will be applied to every element in the array
// DOES NOT MUTATE ORIGINAL ARRAY
----------------------------------------------------------*/
const ages = [12, 24, 11, 35, 59, 18]
const minors = ages.filter(function (num) {
return num < 21
})
// console.log(minors);
/*--------------------------------------------------------*/
/* Find
//
// Returns the first element that passes the test
// Accepts callback function that will be applied to every element in the array
// DOES NOT MUTATE ORIGINAL ARRAY
----------------------------------------------------------*/
const firstFound = ages.find(function (num) {
return num < 21
})
// console.log(firstFound);
/*--------------------------------------------------------*/
/* Every
//
// Returns true or false; Checks if every element in the array passes the test;
// Accepts callback function that will be applied to every element in the array
// DOES NOT MUTATE ORIGINAL ARRAY
----------------------------------------------------------*/
const all = ages.every(function (num) {
return num < 21
})
// console.log(all);
/*--------------------------------------------------------*/
/* Sort
//
// Sorts the array in asc or desc order
// MUTATES ORIGINAL ARRAY
----------------------------------------------------------*/
// return < 0; A, B (keep order)
// return > 0; B, A (switch order)
// Ascending order
ages.sort((a, b) => {
if (a > b) return 1
if (a < b) return -1
})
ages.sort((a, b) => a - b)
// Descending order
ages.sort((a, b) => {
if (a > b) return -1
if (a < b) return 1
})
ages.sort((a, b) => b - a)
/*--------------------------------------------------------*/
/* Map
//
// Returns a new array with modified elements; Can be used in method chaining.
// Accepts callback function that will be applied to every element in the array
// DOES NOT MUTATE ORIGINAL ARRAY
----------------------------------------------------------*/
// i - index of the element
// arr - array which is being looped (useful when chaining)
const double = ages.map(function (age, i, arr) {
return age * 2
})
// console.log(double);
/*--------------------------------------------------------*/
/* forEach
//
// Returns undefined. Great to use if nothing should be returned.
// Accepts callback function that will be applied to every element in the array;
// DOES NOT MUTATE ORIGINAL ARRAY
----------------------------------------------------------*/
ages.forEach(function (age) {
// console.log(age);
})
// console.log(ages); // made no changes to the original array
/*--------------------------------------------------------------------------------------*/
/* Array.from() method
----------------------------------------------------------------------------------------*/
// Creating an array programmatically
const dices = Array.from({ length: 10 }, (el, i) => Math.round(Math.random() * 6))
// _ is a convention; Means that the variable is not used
const dices1 = Array.from({ length: 10 }, (_, i) => i + 1)
console.log(dices, dices1);
// Creating an array from NodeList
const mydivs = Array.from(document.querySelectorAll(".test"))