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

Get the largest value from the array in JS or jQuery

本文关键字:数组 获取 最大值 jQuery JS      更新时间:2023-09-26

使用流程图库绘制一个图形。下面是作为数组的图的xy坐标。

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

我只需要选择y坐标的最大值(即23(。请需要专业人员的支持。

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

在新的浏览器中,您可以使用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 t=plottingPoints[0];
$(plottingPoints  ).each (function (i,n){
if (n[1]>t[1]) t=n;
});

现在,t[1]-是你的答案