美文网首页面试那些事
手写js数组方法

手写js数组方法

作者: _静夜听雨_ | 来源:发表于2021-11-22 16:36 被阅读0次

    闲暇时间,复习一下,手写数组方法,更有利于掌握执行的原理。
    注意:注意挂在原型上的是普通函数,this指向直接调用者,如果是箭头函数,则this指向windows,你会发现--方法无效~~

    forEach

    Array.prototype.my_foreach = function(callback){
        for(let i = 0; i < this.length; i++){
            callback(this[i], i, this);
        }
    }
    

    map

    Array.prototype.my_map = function(callback){
        const res = [];
        for(let i = 0; i < this.length; i++){
            res.push(callback(this[i], i, this));
        }
        return res;
    }
    

    filter

    Array.prototype.my_filter = function(callback){
        const res = [];
        for(let i = 0; i < this.length; i++){
            callback(this[i], i, this) && res.push(this[i]);
        }
        return res;
    }
    

    every

    Array.prototype.my_every = function(callback){
        let flag = true;
        for(let i = 0; i < this.length; i++){
            if(!callback(this[i], i, this)){
                flag = false;
                break;
            }
        }
        return flag;
    }
    

    some

    Array.prototype.my_some = function(callback){
        let flag = false;
        for(let i = 0; i < this.length; i++){
            if(callback(this[i], i, this)){
                flag = true;
                break;
            }
        }
        return flag;
    }
    

    reduce

    Array.prototype.my_reduce = function(callback, initVal){
        let start = 0;
        let pre;//上一次返回的结果
        if(initVal !== undefined){//初始值有或者没有,尤其注意为0的情况
            pre = initVal;
        }else{
            pre = this[0];
            start = 1;
        }
        for(let i = start; i < this.length; i++){
            pre = callback(pre, this[i], i, this);
        }
        return pre;
    }
    

    findIndex

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

    indexOf

    Array.prototype.my_indexOf = function(value){
        for(let i = 0; i < this.length; i++){
            if(this[i] === value){
                return i;
            }
        }
        return -1;
    }
    

    find

    Array.prototype.my_find = function(callback){
        for(let i = 0; i < this.length; i++){
            if(callback(this[i], i, this)){
                return this[i];
            }
        }
        return undefined;
    }
    

    fill

    Array.prototype.my_fill = function(value, start = 0, end){
        const len = end || this.length;
        for(let i = start; i < len; i++){
            this[i] = value;
        }
        return this;
    }
    

    includes

    Array.prototype.my_includes = function(value, start = 0, end){
        const len = end || this.length;
        for(let i = start; i < len; i++){
            if(this[i] === value){
                return true
            }
        }
        return false;
    }
    

    join

    Array.prototype.my_join = function(s = ','){
        let str = `${this[0]}`;
        for(let i = 1; i < this.length; i++){
            str += `${s}${this[i]}`
        }
        return str;
    }
    

    flat

    Array.prototype.my_flat = function(){
        let arr = this;
        while(arr.some((item)=>Array.isArray(item))){
            arr = [].concat(...arr);
        }
        return arr;
    }
    

    splice

    Array.prototype.sx_splice = function (start, length, ...values) {
        if (length === 0) return [];
        length = start + length > this.length - 1 ? this.length - start : length;
        const res = [], tempArr = [...this];
        for (let i = start; i < start + values.length; i++) {
          this[i] = values[i - start];
        }
        this.length = start + values.length;
        if (values.length < length) {
          const diffLen = length - values.length;
          for (let i = start + values.length; i < tempArr.length; i++) {
            this[i] = tempArr[i + diffLen];
          }
          this.length = this.length - diffLen ;
        }
        if (values.length > length) {
          for (let i = start + length; i < tempArr.length; i++) {
            this.push(tempArr[i]);
          }
        }
        for (let i = start; i < start + length; i++) {
          res.push(tempArr[i]);
        }
        return res;
     }
    

    相关文章

      网友评论

        本文标题:手写js数组方法

        本文链接:https://www.haomeiwen.com/subject/yxiwtrtx.html