码迷,mamicode.com
首页 > 编程语言 > 详细

取二维数组最大值

时间:2017-05-23 00:36:56      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:javascrip   i++   current   www   console   合并   null   array   get   

    //取二维数组最大值


    var test=[[1,34],[456,2,3,44,234],[4567,1,4,5,6],[34,78,23,1]];

    //1. junior
    function getMaxOne(arr){
        var tmp=[];
        for(var i=0;i<arr.length;i++){
            tmp[i]=0;
            for(var j=0;j<arr[i].length;j++){

                if(tmp[i]<arr[i][j]){
                    tmp[i]=arr[i][j];
                }
            }
            
        }
        return tmp;
    }
    var rel=getMaxOne(test);
    //console.log(rel)



    //2. middle
    function fungetMax(arr){
        return arr.map(function(currentvalue,index,array){
            return currentvalue.reduce(function(standard,current){
                return standard>current?standard:current;
            });
        })
    

    }
    var rem=fungetMax(test);
    //console.log(rem)



    //3. senior
    function max(arr){
        return arr.map(Function.apply.bind(Math.max,null));
    }




    //基于senior
    function mySenior(arr){
        return arr.map(function(currentValue,index,arr){
            return Math.max.apply(null,currentValue)//apply传入null表示不需要上下文
        })

    }
    var me=mySenior(test)
    //console.log(‘me=‘+me)




    //4. smart 二维数组的最大值
    function smart(arr){
        //将二维乃至多维数组合并
        var newArr=arr.join(‘,‘).split(‘,‘);
        return Math.max.apply(null,newArr);
    }
    var smart=smart(test)
     //console.log(smart)

 

 

 

<html>

参考自<a href=http://www.w3cplus.com/javascript/algorithm-return-largest-numbers-in-arrays.html>大漠</a>

</html>

取二维数组最大值

标签:javascrip   i++   current   www   console   合并   null   array   get   

原文地址:http://www.cnblogs.com/hangsarea/p/6891968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!