How to get biggest array element based on his second sub array value
Perform a descending sort based on the second element of each element of the array.
var array = [[-786, 2], [-783, 1], [-782, 5], [-781, 1], [-779, 2], [-778, 1], [-775, 1], [-774, 1], [-773, 1], [-771, 2], [-769, 1], [-767, 1], [-766, 1], [-763, 2], [-760, 2]];
array.sort(function(a, b) {
return b[1] - a[1];
});
console.log(array[0]);
jsFiddle Demo
Please note that while this solution is terse, using sort just for finding the largest or smallest element would be much slower than a for
loop that finds the largest/smallest element in a single iteration.