JavaScript数组api的简单重构

目录

重构pop方法

重构push方法

重构shift方法

重构unshift方法

 重构forEacht方法

重构every方法

重构some方法

重构filter方法

重构map方法


pop,push,shift,unshift,forEacht,every,some,filter,map的简单重构

重构pop方法

/**
  * pop()从末尾开始删除数组元素
  * 方法:pop() 参数:无   返回值:删除掉的数组元素   修改原数组
  */
Array.prototype.myPop = function() {
    if (this.length > 0) {
        let last = this[this.length - 1]
        this.length--
            return last
    } else {
        return undefined
    }
}
let arr = [1, 2, 3, 4, 5]
let arr1 = [1, 2, 3, 4, 5, 6]
let x = arr.myPop()
let y = arr1.pop()
console.log(y, arr1)
console.log(x, arr)

重构push方法

 /**
   * 新增数组元素 push() 从末尾增加
   * push 参数:想要添加的数组元素  返回值:返回数组的长度  修改原数组
   */
Array.prototype.myPush = function() {
    for (i = 0; i < arguments.length; i++) {
        this[this.length] = arguments[i]
    }
    return this.length
}
let a = [1, 4, 3]
let b = a.myPush('ttt', 'qqq')
console.log(b, a)

重构shift方法

/**
  * shift()开头删除数组元素
  * 参数:无    返回值:返回删除的第一项数组元素    修改原数组
  */
Array.prototype.myShift = function() {
    //获取第一项数组元素
    let first = this[0]
    for (let i = 0; i < this.length; i++) {
        //将后面所有数组元素往前移动一位
        this[i] = this[i + 1]
    }
    //数组长度减1
    this.length--
        //返回第一项
        return first
}
let arr = [1, 2, 3, 4, 5]
let res = arr.myShift()
console.log(res, arr)

重构unshift方法

/**
  * unshift() 从数组开始部分增加元素
  * 参数:增加的数组元素   返回值:新数组的长度   修改原数组
  */
Array.prototype.myUnshift = function() {
    //新数组长度
    let sum = this.length + arguments.length
    for (i = sum; i > 0; i--) {
        //将原数组中数组元素向后移动arguments,length位
        if (i > arguments.length) {
            this[i - 1] = this[i - 1 - arguments.length]
        } else {
            this[i - 1] = arguments[i - 1]
        }
    }
    //返回新数组的长度
    return sum
}
let arr = [1, 2, 3, 4, 5]
let res = arr.myUnshift('tom', 'jjjjj')
console.log(res, arr)

 重构forEacht方法

/**
  * forEach方法 for循环升级版
  * 参数:函数(item,index,arr)   没有返回值 给了返回值也是undefined	不修改原数组
  */
Array.prototype.myForEach = function(callback) {
    //callback ---> function(item,index,arr){console.log(item, index, arr)}
    for (let i = 0; i < this.length; i++) {
        callback(this[i],i,this)
    }
}
let arr = [1, 2, 3, 4, 5]
arr.myForEach(function(item, index, arr) {
    console.log(item, index, arr)
})

重构every方法

/**
  * every方法 判断数组中每一个元素是否符合内部表达式 一项数组元素不满足返回false
  * 参数:函数(item,index,arr)  返回值:true false(只要有一项不满足条件直接跳出循环)	不修改原数组
  */
Array.prototype.myEvery = function(callback) {
    for (let i = 0; i < this.length; i++) {
        if (!callback(this[i], i,this)) {
            return false
        }
    }
    return true
}
let arr = [1, 2, 3, 4, 5]
let res = arr.myEvery(function(item, index) {
    console.log('every')
    return item > 0
})
console.log(res, arr)

重构some方法

/**
  * some方法 比对数组每一项元素是否符合函数内部条件表达式 一项符合返回true 全不符合返回false
  * 参数:函数(item,index,arr){}  	返回值;true flase     不修改原数组
  */  
Array.prototype.mySome = function(callback) {
    for (let i = 0; i < this.length; i++) {
        if (callback(this[i], i, this)) {
            return true
        }
    }
    return false
}

let arr = [1, 2, 3, 4, 5]
let res = arr.mySome(function(item) {
    return item > 7
})
console.log(res, arr)

重构filter方法

/**
  * filter方法 过滤除符合条件的数组元素组成新数组
  * 参数:function(){}  返回符合函数表达式数组元素组成新数组  不修改原数组
  */
Array.prototype.myFilter = function(callback) {
    let newArr = []
    for (let i = 0; i < this.length; i++) {
        if (callback(this[i])) {
            newArr.push(this[i])
        }
    }
    return newArr
}
let arr = [1, 2, 3, 4, 5]
let res = arr.myFilter(function(item) {
    return item > 2
})
console.log(res, arr)

重构map方法

/**
  * map方法 映射 对数组每一个元素进行处理返回一个新的数组
  * 参数:function(item,index,arr){}  返回对每一个数组元素进行处理后的新数组  不修改原数组
  */
Array.prototype.myMap = function(callback) {
    let newArr = []
    for (let i = 0; i < this.length; i++) {
        newArr.push(callback(this[i], i, this))
    }
    return newArr
}
let arr = [18, 23, 17, 22, 46]
let res = arr.myMap(function(item, index, arr) {
    return item * 10
})
console.log(res, arr)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值