How to return array element whose first index is the largest number in JS?


Given the array below, how I can return the element whose first index contains the highest number:

let ar = [[1,24], [2, 6]]

I’ve tried many proposed solutions, but they return the number itself, while it should be, in the case above [2,6]

One of the solutions I’ve tried is, but it returns 24, 6:

var maxRow ={ return Math.max.apply(Math, row); });


To do what you require you can use reduce() to compare the values of the first item in each child array and return the one with the highest value:

let ar = [[1,24], [2, 6]]
let result = ar.reduce((acc, cur) => acc[0] < cur[0] ? cur : acc);  

Answered By – Rory McCrossan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published