-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.js
More file actions
209 lines (178 loc) · 5.09 KB
/
Copy patharray.js
File metadata and controls
209 lines (178 loc) · 5.09 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
/**
* Arrays in javascript
*/
//* How to create a new array
{
[];
Array();
new Array();
new Array(12); // [ <12 empty item> ]
new Array('index1', 'index2', 'index3');
new Array(..."abcdefghijklmnopqrstuvwxyz"); // ['a', 'b', 'c', ...]
new Array(...'123', ...'456', ...'789');
// destructuring
const[,,newArray] = ['Aa', 'Bb', 'Cc', 'Dd'] // 'Cc'
}
//* Examples of arrays
{
[1, 2, 3, 4, 5, 6];
['ali', 'muhammad', 'google', 'github'];
[1, true, null, undefined, NaN, 'func()', {key: 'value'}];
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
}
//* Array validation
{
Array.isArray([]);
}
//* Range of arrays
//* The maximum range of an array is (4294967296) === (2 ** 32)
//* Invalid range of array
// 1) (negative number) === (form -Infinity to -1)
// 2) (floating point number) === (2.23)
{
// maximum range of array
new Array(2 ** 32 - 1) // -1: Because we start from zero, not from one
// Invalid array length
new Array(-1)
new Array(23.5)
}
//* How to get the array size
//* How to set the array size
//* How to set the array size conditionally
{
// get
['test', 1, 100, true].length;
// set
[].length = 5;
// conditional set
if (true){
[].length = 10; // the extra elements are deleted
}
}
//* How to add and remove item from arrays
//* shift(), unshift(), splice(), push(), pop()
{
// Add and remove to the beginning of the array
[].unshift();
[].shift();
// Add and remove to the middle of the array
[/* Your array */].splice(0 /* number of index */, 1);
// Add and remove to the end of the array
[].push();
[].pop();
}
//* Search the array
//* Return (Index, Element, Boolean)
{
// Return (Index)
// return (first and last) index of element : search by value
{
[].indexOf('item'); // It starts searching from the beginning of the array and returns its index as soon as it finds the first similar item.
[].lastIndexOf('item'); // It starts searching from the end of the array and returns its index as soon as it finds the first matching item.
}
// return (first and last) index of element : search by condition
{
[].findIndex(item => item.length >= 0); // Returns the first index whose size is greater than zero
[].findLastIndex(item => item.length >= 0); // Returns the last index whose size is greater than zero
}
// Return (Element)
// return (first) element of array : search by condition
{
[].find(element => element < 0); // Returns the first index whose size is lower than zero
}
// return (all) element of array : search by condition
{
[].filter(element => element > 0); // Return [1, 2, 3, 5], numbers are bigger than 0
}
// Return (Boolean)
// return (boolean) : search by value
{
[].includes('item');
}
// return (boolean) : search by condition
{
[].some(item => item.length > 0); // true: If one of the elements was acceptable to the condition
[].every(item => item.length > 0); // true: If all of the elements was acceptable to the condition
}
}
//* Get the (last and first) elements of the array
{
// Last element
// one item
[].at(-1);
// more than one
[].slice(-2);
// First element
// one item
[].at(1);
// more than one
[].slice(0, 2);
}
//* How to merge two arrays
{
[].concat(new Array(10));
}
//* Operations on array elements
{
// Using map method
{
[].map(index => index + 'test'); // return new array with new elements
// Or
function adder(element){
return element + 'test'
}
[].map(adder);
}
// Using loops
{
for (let i = 0; i < [].length; i++){
[][i] // your operation
}
}
// Using reduce method
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0].reduce((per, current) => per + current); // 45
}
//* Sort the array
{
// By default, the array is sorted alphabetically
[3, 4, 12, 3, 4, 6, 20, 33, 11].sort(); // [11, 12, 20, 3, 3, 33, 4, 4, 6]
// Sort by number (integer and float)
// short mode
[3, 4, 12, 3, 4, 6, 20, 33, 11].sort((a, b) => a - b); // low to high
[3, 4, 12, 3, 4, 6, 20, 33, 11].sort((a, b) => b - a); // high to low
// long mode
[3, 4, 12, 3, 4, 6, 20, 33, 11].sort((a, b) => {
if(a > b) return 1;
if(a === b) return 0;
if(a < b) return -1;
});
}
//* How to use split and join in array
//* Convert string to array
//* Convert array to string
//* split(), join()
{
// use split and join
"a, b, c, d".split(', '); // ['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd'].join(', '); // "a, b, c, d"
// split with: spread operator
[..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
// using for loop
let result = '';
let array = [...'this is testy text']
for (let i = 0; i < array.length; i++) {
result += array[i];
}
}
//* How to reverse an array
{
[1, 2, 3].reverse(); // [3, 2, 1]
}
//* How to copy an array
{
new Array(...[]); // []: It means the array that we want to copy
}