Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

手写数组的方法 #10

Open
ichanghe opened this issue Dec 23, 2021 · 0 comments
Open

手写数组的方法 #10

ichanghe opened this issue Dec 23, 2021 · 0 comments

Comments

@ichanghe
Copy link
Owner

forEach:

Array.prototype.myForEach = function(cb){
    var _arr = this;
    var _len = _arr.length;
    var _arg2 = arguments[1]||window
    for(let i=0;i<_len;i++){
        cb.apply(_arg2,[_arr[i],i,_arr])
    }
}

map和 filter:

Array.prototype.myMap = function(cb){
    var _arr = this;
    var _len = _arr.length;
    var _arg2 = arguments[1]||window
    var _newArr = [];
    var _item;
    var res;
    for(let i=0;i<_len;i++){
        _item = deepClone(arr[i])
        
       _res =  cb.apply(_arg2,[item,i,_arr])
       _res&& _newArr.push(_res)
    }
    return _newArr;
}

Array.prototype.myFilter = function(cb){
    var _arr = this;
    var _len = _arr.length;
    var _arg2 = arguments[1]||window
    var _newArr = [];
    var _item;
    var res;
    for(let i=0;i<_len;i++){
        _item = JSON.parse(JSON.stringify(arr[i])))
        
       cb.apply(_arg2,[_item,i,_arr])?_newArr.push(_item):''
    }
    return _newArr;
}

some:

Array.prototype.mySome = function (cb){
    var _arr = this
    var _length = _arr.length
    var args = arguments[1] || window
    var status = false
    for(let i=0;i<_length;i++){
      let result =  cb.apply(args,[_arr[i],i,_arr])    
      if(result) status = true
    }
    return status
}

every:

Array.prototype.myEvery = function (cb){
    var _arr = this
    var _length = _arr.length
    var args = arguments[1] || window
    for(let i=0;i<_length;i++){
      let result =  cb.apply(args,[_arr[i],i,_arr])    
      if(!result) return false
    }
    return true
}

slice:

Array.prototype.mySlice = function (start, end) {
    var result = new Array();
    start = start || 0;
    end = end || this.length; //this指向调用的对象,当用了call后,能够改变this的指向,也就是指向传进来的对象,这是关键
    for (var i = start; i < end; i++) {
        result.push(this[i]);
    }
    return result;
};

reduce:

Array.prototype.myReduce = function(cb,initialValue){
    var _arr = this
    var _length = _arr.length
    for(var i =0;i<_length;i++){
      	initialValue =  cb.apply(null,[inititalValue,])
    }
    return initialValue
}

reduceRight:

Array.prototype.myReduce = function(cb,initialValue){
    var _arr = this
    var _length = _arr.length
    for(var i =_length;i>0;i--){
      	initialValue =  cb.apply(null,[inititalValue,])
    }
    return initialValue
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant