-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_array.js
More file actions
235 lines (201 loc) · 8.08 KB
/
5_array.js
File metadata and controls
235 lines (201 loc) · 8.08 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* "Array" is just a special type of "Object", with the numerial indices as the
* keys.
*/
let mixedData = [1, true, 'abcd', 'all the strings', null, undefined];
console.log(mixedData);
console.log(Array.isArray(mixedData)); // true
console.log(`Type: ${typeof mixedData}`); // object
console.log();
// *** Array.prototype.length Property ***
console.log(`Length: ${mixedData.length}`); // 6
// Set the length of an array to be:
// > current length: Fill the new positions with "undefined"
mixedData.length += 1;
console.log(`Extended length: ${mixedData.length}`); // 7
console.log(mixedData); // [1, true, 'abcd', 'all the strings', null, undefined, <1 empty item>]
// < current length: Truncate the array
mixedData.length -= 3;
console.log(`Cut length: ${mixedData.length}`); // 4
console.log(mixedData); // [1, true, 'abcd', 'all the strings']
// Thus, Array.length does not necessarily indicate the number of defined values
// in the array.
// Note:
// If you try to access an index in an array that doesn't exist, it will
// automatically return "undefined".
console.log(
"Accessing an index in an array that doesn't exist: " +
`${mixedData[mixedData.length]}`
); // undefined
console.log();
// *** Array.prototype.slice() Method ***
console.log('Original:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings']
// Slice the array from index 1 (inclusive) to index 3 (exclusive)
const sliced = mixedData.slice(1, 3);
console.log('Sliced from index 1 (inclusive) to index 3 (exclusive):');
console.log(sliced); // [true, 'abcd']
// The returned "sliced" is a shallow copy of the original array, and the
// original array is not modified.
console.log(
'Whether the sliced copy is a shallow copy of the original array: ' +
`${Object.is(sliced, mixedData)}`
); // false
console.log();
// *** Array.prototype.indexOf() Method ***
console.log('Original:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings']
// Find the index of the first occurrence of 'abcd' starting from index 1
// (forwards)
console.log(
"Index of the first occurrence of 'abcd' starting from index 1: " +
`${mixedData.indexOf('abcd', 1)}`
); // 2
console.log();
// *** Array.prototype.lastIndexOf() Method ***
console.log('Original:');
console.log(mixedData); // [1, true, 'abc', 'all the strings']
// Find the index of the last occurrence of 1 starting from index 2
// (backwards)
console.log(
'Index of the last occurrence of 1 starting from index 2: ' +
`${mixedData.lastIndexOf(1, 2)}`
); // 0
console.log();
// *** Array.prototype.push() Method ***
console.log('Original:');
console.log(mixedData); // [1, true, 'abc', 'all the strings']
let newLength = mixedData.push('basketball', 'soccer');
console.log('After pushing:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings', 'basketball', 'soccer']
console.log(`New length: ${mixedData.length}`); // 6
console.log();
// *** Array.prototype.pop() Method ***
console.log('Original:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings', 'basketball', 'soccer']
const lastElem = mixedData.pop();
console.log('After popping:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings', 'basketball']
console.log(`Popped last element: ${lastElem}`); // soccer
console.log();
// *** Array.prototype.unshift() Method ***
console.log('Original:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings', 'basketball', 'soccer']
newLength = mixedData.unshift('new first element');
console.log('After appending on the left by unshift() method:');
console.log(mixedData); // ['new first element', 1, true, 'abcd', 'all the strings', 'basketball']
console.log(`New length: ${newLength}`); // 6
console.log();
// *** Array.prototype.shift() Method ***
console.log('Original:');
console.log(mixedData); // ['new first element', 1, true, 'abcd', 'all the strings', 'basketball']
const firstElem = mixedData.shift();
console.log('After popping from the left by shift() method:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings', 'basketball']
console.log(`Popped first element: ${firstElem}`); // new first element
console.log();
// *** Array.prototype.splice() Method ***
console.log('Original:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings', 'basketball']
// To simply add new elements, specify deleteCount as 0:
let removed = mixedData.splice(4, 0, 42);
console.log('After adding 42 to index 4:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings', 42, 'basketball']
console.log('Removed elements:');
console.log(removed); // []
// To simply remove existing elements, do not provide new any items:
removed = mixedData.splice(4, 1);
console.log('After removing one element at index 4:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings', 'basketball']
console.log('Removed elements:');
console.log(removed); // [42]
console.log();
// *** Array.prototype.concat() Method ***
console.log('Original:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings', 'basketball']
console.log(mixedData.contat(['first']));
console.log('After concatenation:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings', 'basketball', 'first']
mixedData.pop();
console.log();
// *** Array.prototype.reverse() Method ***
console.log('Original:');
console.log(mixedData); // [1, true, 'abcd', 'all the strings', 'basketball']
const reversedRef = mixedData.reverse();
console.log('After reversing:');
console.log(reversedRef); // [basketball', 'all the strings', 'abcd', true, 1]
// The returned "reversedRef" is a new reference to the original array, i.e., it
// shadows "mixedData".
console.log(
'Whether the returned reference is a new reference to the original array: ' +
`${Object.is(reversedRef, mixedData)}`
); // true
console.log();
// *** Array.prototype.sort() Method ****
const unsorted = [undefined, 1, 4, 2, 8, 5, 7, 10];
console.log('Unsorted:');
console.log(unsorted);
let sortedCopy = unsorted.sort();
/*
Default compare function: Ascending order in string Unicode code points
(First convert the values to strings, and then do the comparison)
*/
console.log('After sorting:');
console.log(sortedCopy); // [1, 10, 2, 4, 5, 7, 8, undefined] (Note that all the "undefined" elements are sorted to the end of the array)
console.log(
'Whether the returned reference is a new reference to the original array: ' +
`${Object.is(sortedCopy, mixedData)}`
); // true
// To sort the number array in ascending order, we need to pass in our own
// compare function:
sortedCopy = unsorted.sort((a, b) => a - b);
console.log('After sorting using compare function:');
console.log(sortedCopy); // [1, 2, 4, 5, 7, 8, 10, undefined]
console.log();
// *** Array.prototype.some() Method ***
let myArray = [1, 2, 3, 4, 5];
console.log('The array:');
console.log(myArray); // [1, 2, 3, 4, 5]
console.log(myArray.some(val => val === 3)); // true
console.log();
// *** Array.prototype.every() Method ***
console.log('The array:');
console.log(myArray);
console.log(arr.every(val => val > 0)); // true
console.log();
// *** Array.prototype.find() Method ***
console.log('The array:');
console.log(myArray);
console.log(myArray.find(val % 2 === 0)); // 2
console.log();
// *** Array.prototype.findIndex() Method ***
console.log('The array:');
console.log(myArray);
console.log(myArray.findIndex(val % 2 === 0)); // 1
console.log();
// *** Array.prototype.forEach() Method ***
console.log('The array:');
console.log(mixedData); // ['basketball', 'all the strings', 'abcd', true, 1]
mixedData.forEach((val, idx) => console.log(`Logged: [${idx}] ${val}`));
/**
* Logged: [0] basketball
* Logged: [1] all the strings
* Logged: [2] abcd
* Logged: [3] true
* Logged: [4] 1
*/
console.log();
// *** Array.prototype.map() Method ***
console.log('Original array:');
console.log(myArray); // [1, 2, 3, 4, 5]
const newArray = myArray.map(num => num + 100);
console.log('New array:');
console.log(newArray); // [101, 102, 103, 104, 105]
console.log();
// *** Array.prototype.filter() Method ***
myArray = [1, 2, 3, 4, 5];
console.log('Original array:');
console.log(myArray);
const filtered = myArray.filter(num => num > 3);
console.log('New array:');
console.log(filtered); // [4, 5]