2

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

}

Service

CalculateDeltas(){
/* do some math to calculate then save in array Deltas*/

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

 getDeltasRx1(){
        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 :

NaN

Ixam Deirf
  • 425
  • 2
  • 6
  • 14
  • Because you are not using it properly see the [doc](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min) – litelite Aug 16 '17 at 19:28
  • Some of the values could not be converted to a Number, read the docs of Math.min – webdeb Aug 16 '17 at 19:33

1 Answers1

6

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);
console.log(min);

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.

Paul Fitzgerald
  • 11,770
  • 4
  • 42
  • 54