面试那些事

手写js数组方法

2021-11-22  本文已影响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;
 }
上一篇下一篇

猜你喜欢

热点阅读