简体   繁体   English

从JS或jQuery中的数组中获取最大值

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

Using flow chart lib I'm plotting a graph. 使用流程图库,我正在绘制图形。 Below is the x and y coordinates of the graph as an array. 以下是作为数组的图形的xy坐标。

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

I just need to pick largest value of the y coordinate (ie 23). 我只需要选择y坐标的最大值(即23)。 Please need support of expertise. 请需要专业知识的支持。

In new browsers you can make use of ES5's .map method of arrays. 在新的浏览器中,您可以使用ES5的.map数组方法。 Also, Math.max returns the highest value of all arguments: 此外, 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;

});

now , t[1] - is your answer 现在,t [1]-是您的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM