我想从数组中获取最小值。如果数据包含null
值,则Math.min.apply
返回0
值null
。请参阅此 JSFiddle 示例。即使数组中存在空值,如何获得真正的最小值?
代码(与 JSFiddle 示例相同):
var arrayObject= [ {"x": 1, "y": 5}, {"x": 2, "y": 2}, {"x": 3, "y": 9}, {"x": 4, "y": null}, {"x": 5, "y": 12} ];
var max = Math.max.apply(Math, arrayObject.map(function(o){return o.y;}));
var min = Math.min.apply(Math, arrayObject.map(function(o){return o.y;}));
$("#max").text(max);
$("#min").text(min);