js面试题-手写数组的方法

本文主要展示了JavaScript中数组的一些常见方法的实现代码,包括at、concat、every、fill、filter、find、findIndex、flat、forEach、from、includes、indexOf、isArray、join、lastIndexOf、map、of、pop、push、reduce、reverse、shift、slice、some、unshift等。这些方法是JS开发中经常遇到的,了解其工作原理有助于提升编程能力。

首先声明,这里只写代码。至于方法的作用请自行查阅mdn文档。

1、Array.prototype.at()方法

Array.prototype.myAt = function(params = 0){
    let temp = this,
        index,
        result,
    if(typeof params == 'string'){
        if(params.length == 0){
            index = 0
        }else{
            if(isNaN(Number(params))){
                index = 0
            }else{
                index = Number(params) == 0 ? 0 : Number(params) > 0 ? Number(params) : temp.length + Number(params)
            }
        }
    }else if(typeof params == 'boolean'){
        index = Number(params)
    }else if(typeof params == 'number'){
        index = params == 0 ? 0 : params > 0 ? params : temp.length + params
    }else if(JSON.stringify(params) == "[]" || JSON.stringify(params) == '{}'){
        index = 0
    }
    for(let i = 0,l = temp.length; i < l; i++){
        if(i == index){
            result = temp[i]
        }
    }
    return result
}

2、Array.prototype.concat方法

Array.prototype.myConcat = function(...params){
    if(params.length == 0)return this
    reutrn [...this,...params].flat()
}

3、Array.prototype.every方法

Array.prototype.myEvery = function(fn,thisValue = window){
    let temp = this
    if(typeof fn != 'function'){
        throw new Error(`${fn} is not a function`)
    }
    for(let i = 0,l = temp.length; i < l; i++){
        if(!fn.call(thisValue,temp[i],i,temp)){
            return false
        }
    }
    return true
}

4、Array.prototype.fill方法

Array.prototype.myFill = function(value,start = 0,end){
    let temp = this
    let i = start
    let l = end ? (end > temp.length ? temp.length : end) : temp.length
    for(; i < l; i++){
        temp[i] = value
    }
}

5、Array.prototype.filter方法

Array.prototype.myFilter = function(fn,thisValue = window){
    if(typeof fn !== 'function'){
        throw new Error(`${fn} is not a function`)
    }
    let temp = this
    let result = []
    for(let i = 0, l = temp.length; i < l; i++){
        if(fn.call(thisValue,temp[i],i,temp)){
            result.push(temp[i])
        }
    }
    return result
}

6、Array.prototype.find方法

Array.prototype.myFind = function(fn,thisValue = window){
    if(typeof fn !== 'function'){
        throw new Error(`${fn} is not a function`)
    }
    let temp = this
    for(let i = 0, l = temp.length; i < l; i++){
        if(fn.call(thisValue,temp[i],i,temp)){
            return temp[i]
        }
    }
}

7、Array.prototype.findIndex方法

Array.prototype.myFindIndex = function(fn,thisValue = window){
    if(typeof fn !== 'function'){
        throw new Error(`${fn} is not a function`)
    }
    let temp = this
    for(let i = 0, l = temp.length; i < l; i++){
        if(fn.call(thisValue,temp[i],i,temp)){
            return i
        }
    }
}

8、Array.prototype.flat方法

Array.prototype.myFlat = function (dep = 1) {
    return this.reduce((acc, val) => {
        return  acc.concat(Array.isArray(val) && dep > 0 ? val.myFlat(--dep) : Array.isArray(val) ? [val] : val);
    }, [])
}

9、Array.prototype.forEach方法

Array.prototype.myForEach = function(fn,thisValue = window){
    let temp = this
    if(typeof fn !== 'function'){
        throw new Error(`${fn} is not a function`)
    }

    for(let i = 0, l = temp.length; i < l; i++){
        fn.call(thisValue,temp[i],i,temp)
    }
}

10、Array.from方法

Array.myFrom = function(temp){
    return Array.prototype.slice.call(temp)
}

11、Array.prototype.includes方法

Array.prototype.myIncludes = function(item){
    let temp = this
    return temp.indexOf(item) !== -1
}

12、Array.prototype.indexOf方法

Array.prototype.myIndexOf = function(item,start = 0){
    let temp = this
    for(let i = start,l = temp.length - start; i < l; i++){
        if(temp[i] == item){
            return i
        }
    }
}

13、Array.isArray方法

Array.isArray = function(temp){
    return Object.prototype.toString.call(temp) == "[object,Array]"
}

14、Array.prototype.join方法

Array.prototype.myJoin = function(str = ','){
    let temp = this
    let result = temp.reduce((pre,cur) => pre + `${cur}${str}`,'')
    return result.slice(0,result.length - str.length)
}

15、Array.prototype.lastIndexOf方法

Array.prototype.myLastIndexOf = function(params){
    let temp = this
    if(!params || !temp.includes(params)) return -1
    return temp.length - 1 - temp.reverse().indexOf(params)
}

16、Array.prototype.map方法

Array.prototype.myMap = function(fn,thisValue = window){
    if(typeof fn !== 'function'){
        throw new Error(`${fn} is not a function`)
    }

    let temp = this
    let result = []
    
    for(let i = 0, l = temp.length; i < l; i++){
        result.push(fn.call(thisValue,temp[i],i,temp))
    }

    return result
}

17、Array.of方法

Array.myOf = function(){
    return Array.from(arguments)
}

18、Array.prototype.pop方法

Array.prototype.myPop = function(){
    let result = this[this.length - 1]
    this.length--
    return result
}

19、Array.prototype.push方法

Array.prototype.myPush = function(){
    Array.from(arguments).forEach(item => this[this.length] = item ,this)
    return this.length
}

20、Array.prototype.reduce方法

Array.prototype.myReduce = function(fn,initValue){
    if(typeof fn !== 'function'){
        throw new Error(`${fn} is not a function`)
    }

    let acc,
        initIndex = initValue ? 0 : 1,
        temp = this,
        acc = initValue ? initValue : temp[0]

    for(let i = initIndex, l = temp.length; i < l; i++){
        acc = fn(acc,temp[i],i,temp)
    }

    return acc
}

21、Array.prototype.reverse方法

Array.prototype.myReverse = function(){
    for(let i = 0, l = this.length; i < l; i++){
        if(i < l - 1 - i){
            let item = this[i]
            this[i] = this[l - 1 - i]
            this[l - 1 - i] = item
        }       
    }
}

22、Array.prototype.shift方法

Array.prototype.myShift = function(){
    let temp = this
    let item = temp[0]
    temp.splice(0,1)
    return item
}

23、Array.prototype.slice方法

Array.prototype.mySlice = function(begin = 0,end = 0){
    let temp = this
    let result = []
    for(let i = begin, l = end ? end : temp.length; i < l; i++){
        result.push(temp[i])
    }
    return result
}

24、Array.prototype.some方法

Array.prototype.mySome = function(fn,thisValue = window){
    if(typeof fn !== 'function'){
        throw new Error(`${fn} is not a function`)
    }
    let temp = this
    for(let i = 0, l = temp.length; i < l; i++){
        if(fn.call(thisValue,temp[i],i,temp)){
            return true
        }
    }
    return false
}

25、Array.prototype.unshift方法

Array.prototype.myUnShift = function(...items){
    this.reverse().push(...items.reverse())
    this.reverse()
    return this.length
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值