写些经常使用的内置方法的实现,最近心态不太ok,看成发泄好了。数组
先给出要实现的方法列表,将它们放在原型上:函数
Array.prototype.myBubbleSort = myBubbleSort //冒泡排序实现
Array.prototype.myForEach = myForEach //forEach实现
Array.prototype.myMap = myMap //map实现
Array.prototype.myFilter = myFilter //过滤器实现
Array.prototype.myReduce = myReduce //累加器实现
Array.prototype.myEvery = myEvery //测试数组的全部元素是否都经过了指定函数的测试
Array.prototype.mySome= mySome //测试是否至少有一个元素经过由提供的函数实现的测试
复制代码
你们都在是在数组的原型上,咱们都拿给定的数组说事好了:测试
let arr = [24, 65, 21, 89, 34, 4, 11, 6, 90, 45]
let objectArr = [
{sort: 82},
{sort: 2},
{sort: 64},
{sort: 72},
{sort: 11},
{sort: 56},
{sort: 31},
{sort: 27},
{sort: 46},
{sort: 7}
]
复制代码
js的排序内部是用一个快排,咱们在这里不使用快排,先使用个冒泡排序。ui
具体排序过程为:this
- 将整个待排序的记录序列划分红有序区和无序区,初始状态有序区为空,无序区包括全部待排序的记录。
- 对无序区从前向后依次将相邻记录的关键码进行比较,若反序则交换,从而使得关键码小的记录向前移,关键码大的记录向后移(像水中的气泡,体积大的先浮上来)。
- 重复执行(2),直到无序区中没有反序的记录。
咱们来写个js版本的:spa
function bubbleSort(arr){
let exchange = arr.length //用来记录上次最后交换位置,后续不用再作重复比较
while (exchange) {
let bound = exchange - 1
exchange = 0
for (let i = 0; i< bound; i++){
if(arr[i] > arr[i+1]){
let item = arr[i]
arr[i] = arr[i+1]
arr[i + 1] = item
exchange = i
}
}
}
return arr
}
console.log(bubbleSort(arr)) //[ 4, 6, 11, 21, 24, 34, 45, 65, 89, 90 ]
复制代码
为了更符合咱们平时js的链式使用习惯,咱们进行以下改造:prototype
function myBubbleSort(fn){
let exchange = this.length -1
while (exchange) {
let bound = exchange
exchange = 0
for (let i = 0; i< bound; i++){
if(fn(this[i], this[i + 1]) > 0){
let item = this[i]
this[i] = this[i+1]
this[i + 1] = item
exchange = i
}
}
}
return this
}
Array.prototype.myBubbleSort = myBubbleSort
console.log(arr.myBubbleSort((a,b) => a-b)) // [ 4, 6, 11, 21, 24, 34, 45, 65, 89, 90 ]
console.log(objectArr.myBubbleSort((a,b) => {a.sort - b.sort})) //[ { sort: 82 },{ sort: 2 },{ sort: 64 },{ sort: 72 },{ sort: 11 },{ sort: 56 },{ sort: 31 },{ sort: 27 },{ sort: 46 },{ sort: 7 } ]
复制代码
function myForEach(Fn){
for (let i=0; i<this.length; i++){
Fn(this[i], i)
}
return this
}
Array.prototype.myForEach = myForEach
objectArr.myForEach((item, index) => {
item.index = index
})
console.log(objectArr) //[ { sort: 82, index: 0 },{ sort: 2, index: 1 },{ sort: 64, index: 2 },
//{ sort: 72, index: 3 },{ sort: 11, index: 4 },{ sort: 56, index: 5 },{ sort: 31, index: 6 }...
复制代码
function myMap(Fn){
let arr = []
for (let i=0; i<this.length; i++) {
arr.push(Fn(this[i], i))
}
return arr
}
Array.prototype.myMap = myMap
let mapArr = objectArr.myForEach((item, index) => {
item.index = index
}).myMap(item => {
return item.index
})
console.log(mapArr) //[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
复制代码
function myFilter(Fn){
let arr = []
this.forEach((item, index) => {
if(Fn(item, index, this)){
arr.push(item)
}
})
return arr
}
Array.prototype.myFilter = myFilter
objectArr.myFilter(item => {
return item.sort % 2 === 0
}) //[ { sort: 82 },{ sort: 2 },{ sort: 64 },{ sort: 72 },{ sort: 56 },{ sort: 46 } ]
arr.myFilter(item => {
return item % 2 === 0
}) //[ 24, 34, 4, 6, 90 ]
复制代码
function myReduce(Fn, initialValue = 0){
this.forEach((item, index) => {
initialValue = Fn(initialValue, item, index, this)
})
return initialValue
}
Array.prototype.myReduce = myReduce
let reduceArr = objectArr.myReduce((intValue, item) => {
return intValue + item.sort
}, 2)
console.log(reduceArr) // 400
复制代码
function myEvery(Fn){
for (let i = 0; i<this.length; i++){
if(!Fn(this[i])){
return false
}
}
return true
}
Array.prototype.myEvery = myEvery
let isEvery = objectArr.myEvery(item => {
return item.sort > 20
})
console.log(isEvery) //false
复制代码
function mySome(Fn){
for (let i = 0; i<this.length; i++){
if(Fn(this[i])){
return true
}
}
return false
}
Array.prototype.mySome= mySome
let isSome1 = objectArr.mySome(item => {
return item.sort > 80
})
console.log(isSome1) //true
let isSome2 = objectArr.mySome(item => {
return item.sort > 100
})
console.log(isSome2) //false
复制代码