【基础】重写foreach&map&filter&every&some&reduce&reduceRight

1. 重写foreach


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

const arr = [1,2,3,4,5,6];
arr.myForeach((item,i)=>{
    console.log(item,i);
})

2.重写map

Array.prototype.myMap = function (cb) {
    var _this = this;
    var _len = _this.length;
    var _args = arguments[1] || window;
    var res = [];
    for (var i = 0; i < _len; i++) {
        res.push(cb.call(_args, _this[i], i, _this));
    }
    return res;
}

const arr = [1, 2, 3, 4, 5];
const arr2 = arr.myMap(item => item * 2);
console.log(arr2, arr);

3.重写filter

Array.prototype.myFilter = function (cb) {
    var _this = this;
    var _args = arguments[1] || window;
    var _len = _this.length;
    var res = [];
    for (var i = 0; i < _len; i++) {
        cb.call(_args, _this[i], i, _this) ? res.push(_this[i]) : null;
    }
    return res;
}

const arr = [1, 2, 3, 4, 5, 6];
const arr2 =  arr.myFilter(item => item % 2 == 0);
console.log(arr2);

4.重写every

Array.prototype.myEvery = function(cb){
    var _this = this;
    var _len = _this.length;
    var _arg2 = arguments[1] || window;
    
    for(var i = 0;i<_len;i++){
        if(!cb.call(_arg2,_this[i],i,_this)){
            return false;
        }
    }
    return true;
}

const arr = [2,4,6,8,9];
const res = arr.myEvery(item=>item%2===0)
console.log(res);

5.重写some

Array.prototype.mySome = function(cb){
    var _this = this;
    var _len = _this.length;
    var _arg2 = arguments[1] || window;
    
    for(var i = 0;i<_len;i++){
        if(cb.call(_arg2,_this[i],i,_this)){
            return true;
        }
    }
    return false;
}

const arr = [2,4,6,8,23];
const res = arr.mySome(item=>item%2===0)
console.log(res);

6.重写reduce

Array.prototype.myReduce = function (cb, initialValue) {
    var _this = this;
    var _len = _this.length;
    var _arg3 = arguments[2] || window;

    for (var i = 0; i < _len; i++) {
        initialValue = cb.call(_arg3, initialValue, _this[i], i, _this);
    }
    return initialValue;
}

var initialValue = 0;
var arr = [1, 2, 3, 4];
var res = arr.myReduce(function (prev, item, index, array) {
    console.log(index);
    return prev +item;
}, initialValue)

console.log(res);

7.重写reduceRight


Array.prototype.myReduceRight = function (cb, initialValue) {
    var _this = this;
    var _len = _this.length;
    var _arg3 = arguments[2] || window;

    for (var i = _len; i >= 0; i--) {
        initialValue = cb.call(_arg3, initialValue, _this[i], i, _this);
    }
    return initialValue;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值