Get largest value in multi-dimensional array javas

2019-01-11 14:46发布

I have an array that looks like the following:

array = [[1, 5], [4, 7], [3, 8], [2, 3],  
 [12, 4], [6, 6], [4, 1], [3, 2], 
 [8, 14]]

What I need is the largest number from the first value of the sets, so in this case 12. Looking at some examples online, the best way I saw to accomplish this is :

Math.max.apply Math, array

Problem is, this only works with single dimensional arrays. How would I impliment this for my senario? (jquery allowed)


The end solution:

It wasn't part of the question, but I needed both the min and max from the array, and that changes things a little.

    unless device.IE
        justTheDates    = magnitudeArray.map (i) -> i[0]
        @earliest       = Math.min.apply Math, justTheDates
        @latest         = Math.max.apply Math, justTheDates                 
    else
        @earliest       = magnitudeArray[0][0]
        @latest         = magnitudeArray[0][0]
        for magnitudeItem in magnitudeArray
            @earliest   = magnitudeItem[0] if magnitudeItem[0] < @earliest
            @latest     = magnitudeItem[0] if magnitudeItem[0] > @latest

8条回答
Lonely孤独者°
2楼-- · 2019-01-11 14:58

Sample Input: largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

function largestOfFour(arr) {

    var largest = 0;
    var largestArr = [];
    for(var i=0; i<arr.length; i++){
      for(var j=0; j<arr[i].length; j++){

        if(largest < arr[i][j]){
          largest = arr[i][j];
        }
        largestArr[i] = largest;
      }      
      largest = 0;
    }

  return largestArr;
}

You can populate largest numbers into new Array from two dim array.

查看更多
SAY GOODBYE
3楼-- · 2019-01-11 15:00
Array.prototype.maxX = function(){
  return Math.max.apply(Math,this.map(function(o){return o[0];}));
};
查看更多
forever°为你锁心
4楼-- · 2019-01-11 15:05

A simple solution using Underscore.js' max that avoids generating an intermediate array:

max = _(array).max(_.first)[0]

(JSFiddle)

查看更多
一夜七次
5楼-- · 2019-01-11 15:06

http://jsfiddle.net/zerkms/HM7es/

var max = Math.max.apply(Math, arr.map(function(i) {
    return i[0];
}));​

So at first you use array.map() to convert the 2-dimensional array to a flat one, and after that use Math.max()

查看更多
劳资没心,怎么记你
6楼-- · 2019-01-11 15:07

Also, look at _underscore.js. Here is a link to the function _max().

  • It is simply more efficient to read, write and maintain.

The best part about _underscore is that there are about another hundred helper functions similar to _max. Like sort.

Compare the syntax below:

var sortedObject = _.sortBy(object, function(val, key, object) {
    return val;
});

They are easy to chain, and interpret! (Like Douglas Crockford might suggest)

An excellent JSFIDDLE, was provided in this post by @Raynos.

If you are consistently conducting array operations with raw JavaScript, check out _underscore.js, it can greatly simplify your code.

Hope that helps, All the best! Nash

查看更多
狗以群分
7楼-- · 2019-01-11 15:13

You can use .reduce()...

array.reduce(function(max, arr) { 
    return Math.max(max, arr[0]); 
}, -Infinity)

Here's a version that doesn't use Math.max...

array.reduce(function(max, arr) {
    return max >= arr[0] ? max : arr[0];
}, -Infinity);

...and a jsPerf test.

查看更多
登录 后发表回答