簡體   English   中英

從JS或jQuery中的數組中獲取最大值

[英]Get the largest value from the array in JS or jQuery

使用流程圖庫,我正在繪制圖形。 以下是作為數組的圖形的xy坐標。

var plottingPoints  = [[0, 3], [4, 8], [8, 5], [9, 23], [10, 2]];

我只需要選擇y坐標的最大值(即23)。 請需要專業知識的支持。

在新的瀏覽器中,您可以使用ES5的.map數組方法。 此外, Math.max返回所有參數的最大值:

// calculate max value of an array of numbers
Math.max.apply(null, plottingPoints.map(function(element) {
                                            // return y coordinate
                                            return element[1];
                                        }));
var plottingPoints  = [[0, 3], [4, 8], [8, 5], [9, 23], [10, 2]];
var length = plottingPoints.length;
var maxY = -Infinity;
for(var i = 0; i < length; i++)
    maxY = Math.max(plottingPoints[i][1], maxY);
var t=plottingPoints[0];
$(plottingPoints  ).each (function (i,n){

if (n[1]>t[1]) t=n;

});

現在,t [1]-是您的答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM