I would like to get minimum value from an array. If the data contains null value, Math.min.apply returns 0 for null value. Please see this JSFiddle example. How can I get true minimum value even if null value exists in array?
Code (same as in JSFiddle example):
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);
Math.minfunction performs (the internal function)ToNumber()on each array element, which in turn coerces the values to Number. Coercion sucks, we knew that.:Pundefined, which in turn turns toNaNwhich is easily filtered out with.filter(isFinite)