JavaScript中的数组提供了很多原生方法,今天模拟实现一些常用的数组API(forEach,map,reduce,every,filter,find,indexOf,some)
1.forEach
array.forEach(function(currentValue, index, arr), thisValue)
Array.prototype._forEach = function (fn, thisValue) {
let index = 0;
let arr = thisValue || this;
if (typeof fn !== 'function') {
throw new TypeError(fn + 'is not a function')
}
while (index < arr.length) {
if (index in arr) {
fn.call (thisValue, arr[index], index, arr)
}
index ++
}
};
let arr = [1, 3, 5, 7]
arr._forEach((item, i , arr) =>{
console.log('item:' + item + 'i:' + i)
})
arr._forEach(async v => {
await fetch(v)
})
Array.prototype._forEach = async function (fn, thisValue) {
let index = 0;
let arr = thisValue || this;
if (typeof fn !== 'function') {
throw new TypeError(fn + 'is not a function')
}
while (index < arr.length) {
if (index in arr) {
try {
await fn.call(thisValue, arr[index], index, arr)
} catch (e) {
console.log(e)
}
}
index ++
}
}
let arr = [1, 3, 5, 7]
map的实现和forEach大体类似,只是map会返回一个新的数组
array.map(function(currentValue,index,arr),thisValue)
Array.prototype._map = function(fn,thisValue){
let arr = thisValue || this
let index = 0
let newArr = [];
if(typeof fn !== 'function'){
throw new TypeError(fn + 'is not function')
}
while(index<arr.length){
if(index in arr){
let result = fn.call(thisValue, arr[index], index, arr)
newArr[index] = result
}
index++
}
return newArr
}
let arr = [1,3,5,7]
let newArr = arr._map((item)=>{
return item*3
})
console.log(newArr)
filter一般用来筛选,会创建一个新的数组
arr.fillter(function(currentValue,index,arr),thisValue)
Array.prototype._filter = function(fn, thisValue){
let arr = thisValue || this
let newArr = []
if(typeof fn !== 'function'){
throw new TypeError(fn + 'is not function')
}
if(arr.length>0){
for(let i=0;i<arr.length;i++){
if(fn.call(arr,arr[i],i,thisValue)){
newArr.push(arr[i])
}
}
}
return newArr
}
let arr = [1,3,5,7]
let newArr = arr._filter((item)=>{
return item>3
})
console.log(newArr)
reduce()接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算一个值。同时reduce()可以作为一个高阶函数,用于函数的compose.
arr.reduce(function(total,currentValue,currentIndex,arr),initialValue)
Array.protoType._reduce= function(fn,initialValue){
let arr = this
if(typeof fn !== 'function'){
throw new TypeError(fn+ 'is not function')
}
if(!arr.length){
return
}
let newArr = initialValue || 0
for (let i = 0; i < arr.length; i ++){
newArr = fn.call(arr,arr[i],i,arr)
}
return newArr
}
let arr = [1,3,5,7]
function getSum(total,currentValue,currentIndex,arr){
return total + currentValue
}
let newArr = arr._reduce(getSum,2)
console.log(newArr)
slice()可从已有数组中返回选定的元素,可提取字符串的某个部分,并返回新的字符串
array.slice(start,end)
Array.prototype._slice = function(start,end){
let newArr = []
for(let i=0; i< start+end; i++){
newArr.push(this[i])
}
return newArr
}
let arr = ['A','B','C','D','E']
let newArr =arr._slice(2,4)
console.log(newArr)
splice()用于添加或删除数组的方法,改变原数组
arr.splce(index,howmany, item1,....,itemx)
Array.prototype._splice= function(index,howmany=0){
let arr = this
let left = arr.slice(0,index)
let right = arr.slice(index+ howmany,arr.length)
let subArr = Array.prototype.slice.call(arguments,2)
let result = []
result = [...left,...right,...subArr]
for(let i = 0;i<result.length;i++){
this[i] = result[i]
}
return this.slice(index,index+howmany)
}
let arr = ['a','b','c','d']
let result =arr._splice(2,1,'e','f')
console.log(result)
console.log(arr)