-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.js
50 lines (38 loc) · 956 Bytes
/
Array.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
class myArray {
constructor() {
this.data = {};
this.length = 0;
}
get(index) {
return this.data[index];
}
push(item) {
this.data[this.length] = item;
this.length++;
return this.length;
}
pop() {
delete this.data[this.length - 1];
this.length--;
return this.length;
}
unshift(item) {
for (let i = 0; i < this.length; i++) {
this.data[this.length - i] = this.data[this.length - i - 1];
}
this.data[0] = item;
this.length++;
return this.length;
}
delete(index) {
if (index >= 0 && index < this.length) {
for (let i = index; i < this.length; i++) {
this.data[i] = this.data[i + 1];
}
delete this.data[this.length - 1];
this.length--;
}
return this.length;
}
}
const arr = new myArray();