寫些經常使用的內置方法的實現,最近心態不太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
複製代碼