Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
154
rated 0 times [  155] [ 1]  / answers: 1 / hits: 16601  / 10 Years ago, wed, april 30, 2014, 12:00:00

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!!


More From » arrays

 Answers
53

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);

[#71228] Tuesday, April 29, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
payne

Total Points: 527
Total Questions: 108
Total Answers: 88

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
payne questions
Thu, Sep 3, 20, 00:00, 4 Years ago
Tue, Aug 18, 20, 00:00, 4 Years ago
Thu, Oct 11, 18, 00:00, 6 Years ago
;