使用流程圖庫我正在繪製一個圖。下面是該圖的x
和y
座標作爲數組。在JS或jQuery中獲取數組中最大的值
var plottingPoints = [[0, 3], [4, 8], [8, 5], [9, 23], [10, 2]];
我只需要挑選最大值的y
座標(即23)。請需要專業知識的支持。
使用流程圖庫我正在繪製一個圖。下面是該圖的x
和y
座標作爲數組。在JS或jQuery中獲取數組中最大的值
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);
var t=plottingPoints[0];
$(plottingPoints ).each (function (i,n){
if (n[1]>t[1]) t=n;
});
現在,T [1] - 是你的答案
在新的瀏覽器,您可以利用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];
}));