Subjective Effect - 1 year ago 159

Javascript Question

I know this gets asked again and again but hear me out - this question is slightly different.

I can get a max or min from a 1D array like this:

`var w_max = Math.max.apply(Math, myArray);`

var w_min = Math.min.apply(Math, myArray);

But my array is of the type:

`[[[1, 112.0],[2,5.12],[3,113.1],[4,33.6],[5,85.9],[6,219.9]]]`

//max = 219.9, min = 5.12

and I need to get min and max of the second value, with the first value being an index of sorts. I've tried

`myArray[][], myArray[0][0], myArray[[1]]`

and more. Console logging shows I'm getting NaN or -Infinity!!

Answer Source

You can map the array to the second values of the elements:

```
var arr = [[[1, 112.0],[2,5.12],[3,113.1],[4,33.6],[5,85.9],[6,219.9]]];
var values = arr[0].map(function(elt) { return elt[1]; });
var max = Math.max.apply(null, values);
var min = Math.min.apply(null, values);
```