数组forEach,filter,map,reduce

foreach不能中途break和return,若有需求用for循环


<script type = "text/javascript">
        var arr = [1,2,3,4,5];
        // 1.forEach 
        //封装forEach
        Array.prototype.myForEach = function(func){
            for(var i=0; i<this.length;i++){
                func(this[i],i);
            }
        }
        //使用格式
        arr.forEach(function(ele,index){
            //ele 元素
            //index 索引
        });
        
        // 2.filter
        // 不改变原始数组
        //封装filter
        Array.prototype.myFilter = function(func){
            var arr = [];
            for(var i=0;i<this.length;i++){
                if(func(this[i],i)){
                    arr.push(this[i]);
                }
            }            
            return arr;
        }
        //使用格式
        var newArr = arr.myFilter(function(ele,index){
            if(index % 2 ==0){
                return true;//返回ture时,在newArr中添加此元素
            }else{
                return false;//返回false时,此元素过滤,newArr中不会出现此元素
            }
        });
        console.log(newArr);//[1,3,5]
        
        //3.map
        //不会改变原始数组,深度克隆或处理
        //封装myMap
        Array.prototype.myMap = function(func){
            var arr = [];
            for(var i = 0; i< this.length; i++){
                if(this[i]&& typeof(this[i])== 'object'){
                    var newObj = {};
                    deepclone(newObj,this[i]);
                    arr.push(func(newObj,i));
                }else{
                    arr.push(func(this[i],i));
                } 
            }
            return arr;
        }
        //深度克隆
        function deepclone(target,option){
            if(option != null){
                var str,temp;
                for(temp in option){
                    if(option[temp] && typeof(option[temp]) == 'object'){
                        if(Object.prototype.toString.call(option[temp])=="[object Array]"){
                            str = [];
                        }else{
                            str = {};
                        }
                        target[temp] = deepclone(str,option[temp]);
                    }else{
                        target[temp] = option[temp];
                    }
                }
            }else{
                target = null;
            }
            return target;
        }
        //使用格式
        var obj1 = [1,2,3,{hobby:{sing:'good',dance:'well'}}],
            obj2 = {};
        obj2 = obj1.myMap(function(ele,index){
            return ele;
        })
        // console.log(obj2);
        //4.reduce 常做累加器
        //封装myReduce
        Array.prototype.myReduce = function(func,init){
            var k = 0;
            if(init===undefined){
                k = 1;
                preValue = this[0];
            }
            for(k;k<arr.length;k++){
                preValue = func(preValue,this[k],k);
            }
            return preValue;
        } 
        //使用格式
        var value = arr.reduce(function(preValue,ele,index){
            // console.log(ele);
            return preValue+ele;
        })
        // console.log(value);//15

        //5.reduceRight 从右向左遍历
        var value1 = arr.reduceRight(function(preValue,ele,index){
            console.log(index);

        })


    </script>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值