Ixam Deirf Ixam Deirf -3 years ago 144
Javascript Question

Math.min throw Nan

Hello im trying to use Math.min but i am getting NaN as a result, this is the code.

method using Math.min

method() {

var DeltaPrueba = this._markerService.getDeltasRx1();
console.log(DeltaPrueba); // 1
var menor = Math.min(DeltaPrueba);
console.log(menor); //1



/* do some math to calculate then save in array Deltas*/

Deltas = [DeltaT1,DeltaT2,DeltaT3,DeltaT4,DeltaT1CR,DeltaT2CR,DeltaT3CR,DeltaT4CR] ;

return Deltas;

Console log number 1 show the array with all de data like this:

["18.20", "0.00", "425.97", "316.87", "667.80", "422.34", "425.99", "316.89"]

Console log number 2 show the NaN :


Answer Source

You need to do the following

var array = ["18.20", "0.00", "425.97", "316.87", "667.80", "422.34", "425.99", "316.89"];
var min = Math.min.apply(Math, array);

To convert the whole array from strings to numbers you can do array.map(Number) but this won't have any impact on getting the min value, as behind the scenes javascript will convert each of these strings to numbers.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download