Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to select an object by highest property value

I have an object:

[
   {
      name: "first name",
      rolePosition: 85
   },
   {
      name: "second name",
      rolePosition: 91
   }
]

How to select an object with the highest rolePosition value? In this situation is 91

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem can be solved with Array.reduce() as follows:

const arr = [{ name: "first name", rolePosition: 85 }, { name: "second name", rolePosition: 91 }];

const result = arr.reduce((prev, curr) => prev.rolePosition > curr.rolePosition ? prev : curr , {});

console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading