-4

I have an array like this:

const persons = [
  {id: 28, name: 'John'},
  {id: 43, name: 'Doe'},
  {id: 15, name: 'Marcelina'},
  {id: 36, name: 'Frank'},
  {id: 81, name: 'Philips'},
  {id: 57, name: 'Brad'},
];

And expecting result like this:

{id: 81, name: 'Philips'}

How to achieve this with ES6 syntax?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • This might help: https://stackoverflow.com/questions/48786855/es6-find-the-maximum-number-of-an-array-of-objects – JoshG Jun 22 '20 at 09:36

2 Answers2

2

const persons = [
  {id: 28, name: 'John'},
  {id: 43, name: 'Doe'},
  {id: 15, name: 'Marcelina'},
  {id: 36, name: 'Frank'},
  {id: 81, name: 'Philips'},
  {id: 57, name: 'Brad'},
];

const maximum = persons.reduce(function (prev, curr) {
  return prev.id > curr.id ? prev : curr
})
  
console.log(maximum)
Tps
  • 194
  • 5
0

persons = [
  {id: 28, name: 'John'},
  {id: 43, name: 'Doe'},
  {id: 15, name: 'Marcelina'},
  {id: 36, name: 'Frank'},
  {id: 81, name: 'Philips'},
  {id: 57, name: 'Brad'},
];

console.log(
  persons.reduce(
    (maxAcc,cur)=>((maxAcc.id>cur.id)?(maxAcc):(cur))
  )
);
iAmOren
  • 2,760
  • 2
  • 11
  • 23