map

1
2
3
4
5
6
7
8
9
10
11
Array.prototye.map = function (cb) {
if(typeof cb !== "function") {
throw new Error("callback is not function")
return
}
let arr = []
for(let i = 0; i < this.length; i++) {
arr.push(cb(this[i]))
}
return arr
}

filter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Array.prototye.filter = function (cb) {
if(typeof cb !== "function") {
throw new Error("callback is not function")
return
}
let arr = []
for(let i = 0; i < this.length; i++) {
let flag = cb(this[i])
if(flag) {
arr.push(this[i])
}
}
return arr
}

reduce

1
2
3
4
5
6
7
8
9
10
11
Array.prototye.
= function (cb, total) {
if(typeof cb !== "function") {
throw new Error("callback is not function")
return
}
for(let i = 0; i < this.length; i++) {
total = cb(total, this[i])
}
return total
}

some

1
2
3
4
5
6
7
8
9
10
11
12
13
Array.prototye.some = function (cb) {
if(typeof cb !== "function") {
throw new Error("callback is not function")
return
}
for(let i = 0; i < this.length; i++) {
let flag = cb(this[i])
if(flag) {
return flag
}
}
return false
}

every

1
2
3
4
5
6
7
8
9
10
11
12
13
Array.prototye.every = function (cb) {
if(typeof cb !== "function") {
throw new Error("callback is not function")
return
}
for(let i = 0; i < this.length; i++) {
let flag = cb(this[i])
if(!flag) {
return flag
}
}
return true
}