Let's suppose that we have the following array of arrays, each of one containing a pair of values:
javascriptlet ceos = [['Elon', 1971], ['Jeff', 1964], ['Bill', 1955]];
We can use the reduce()
function to find max and min:
javascriptlet max = ceos.reduce((acc, ceo) => acc = ceo[1] > acc[1] ? ceo : acc); let min = ceos.reduce((acc, ceo) => acc = ceo[1] < acc[1] ? ceo : acc); console.log(`The youngest CEO: ${max}`); // The youngest CEO: Elon, 1971 console.log(`The oldest CEO: ${min}`); // The oldest CEO: Bill, 1955
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.