javascript 在多维数组javascript或coffeescript中获取最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11149843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:12:28  来源:igfitidea点击:

Get largest value in multi-dimensional array javascript or coffeescript

javascriptjqueryarrayscoffeescript

提问by Fresheyeball

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 :

我需要的是集合的第一个值中的最大数,所以在这种情况下12. 在网上查看一些示例,我看到的实现此目的的最佳方法是:

Math.max.apply Math, array

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

问题是,这只适用于一维数组。我将如何为我的 senario 暗示这一点?(jquery 允许)



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

回答by

You can use .reduce()...

您可以使用.reduce()...

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


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

这是一个不使用的版本Math.max......

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

...and a jsPerf test.

...和jsPerf 测试

回答by zerkms

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

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()

所以一开始你使用array.map()将二维数组转换为平面数组,然后使用Math.max()

回答by epidemian

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

使用 Underscore.js'max避免生成中间数组的简单解决方案:

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

(JSFiddle)

( JSFiddle)

回答by Sarah-Jane

Array.prototype.maxX = function(){
  return Math.max.apply(Math,this.map(function(o){return o[0];}));
};

回答by BitAtWork

I know this is an old post, but if you (or someone else) want the largest number in the whole array, try with:

我知道这是一篇旧帖子,但如果您(或其他人)想要整个数组中的最大数字,请尝试:

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

var max = array.reduce(function (max, arr) {
    return max >= Math.max.apply(max, arr) ? max : Math.max.apply(max, arr);
}, -Infinity);
console.log(max);

In this examlpe, it will return the value 14.

在本例中,它将返回值 14。

回答by jimr

Using a comprehension in CoffeeScript:

在 CoffeeScript 中使用理解:

Math.max.apply Math, (x[0] for x in array)

Running example

运行示例

回答by Hyman Stone

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

另外,看看_underscore.js。这是函数 _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.

_underscore 最好的部分是还有大约一百个类似于 _max 的辅助函数。喜欢排序。

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)

它们易于链接和解释!(就像道格拉斯·克罗克福德(Douglas Crockford)可能建议的那样)

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

@Raynos这篇文章中提供了一个优秀的 JSFIDDLE

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

如果您一直使用原始 JavaScript 进行数组操作,请查看 _underscore.js,它可以极大地简化您的代码。

Hope that helps, All the best! Nash

希望能帮到你,祝一切顺利!纳什

回答by dorgo

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

样本输入:maximumOfFour([[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.

您可以将最大的数字从两个暗数组填充到新数组中。