-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.js
116 lines (106 loc) · 2.52 KB
/
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
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
Array.prototype.myForEach = function(callback) {
for(let i = 0; i < this.length; i++) {
callback(this[i], i, this)
}
}
Array.prototype.myMap = function(callback) {
let res = []
for (let i = 0; i < this.length; i++) {
let r = callback(this[i], i, this)
res.push(r)
}
return res
}
Array.prototype.myFilter = function(callback) {
let res = []
for (let i = 0; i < this.length; i++) {
let flag = callback(this[i], i, this)
flag && res.push(this[i])
}
return res
}
Array.prototype.mySome = function(callback) {
let flag = false
for (let i = 0; i < this.length; i++) {
flag = callback(this[i], i, this)
if (flag) {
break
}
}
return flag
}
Array.prototype.myEvery = function(callback) {
let flag = true
for (let i = 0; i < this.length; i++) {
flag = callback(this[i], i, this)
if (!flag) {
break
}
}
return flag
}
Array.prototype.myReduce = function(callback, ...args) {
let start = 0
let pre
if (args.length) {
pre = args[0]
} else {
pre = this[0]
start = 1
}
for (let i = 0; i < this.length; i++) {
pre = callback(pre, this[i], i, this)
}
return pre
}
Array.prototype.myFindIndex = function(callback) {
let index = -1
for (let i = 0; i < this.length; i++) {
let flag = callback(this[i], i, this)
if (flag) {
index = i
break
}
}
return index
}
Array.prototype.myFind = function(callback) {
for (let i = 0; i < this.length; i++) {
if (callback(this[i], i, this)) {
return this[i]
}
}
return undefined
}
Array.prototype.myFill = function(val, start = 0, end) {
end = end || this.length
for (let i = start; i < end; i++) {
this[i] = val
}
return this
}
Array.prototype.myIncludes = function(val, start) {
start = start || 0
let flag = Number.isNaN(val)
for (let i = start; i < this.length; i++) {
if (this[i] === val || Number.isNaN(this[i] === flag)) {
return true
}
}
return false
}
Array.prototype.MyJoin = function(s = ',') {
let str = ''
for (let i = 0; i < this.length - 1; i++) {
str += this[i] + s
}
str += this[this.length - 1]
return str
}
Array.prototype.myFlat = function(num = 1) {
let arr = []
while(this.some(item => Array.isArray(item))){
arr.concat(this)
}
return arr
}