Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
How to in JS find in an array of objects two objects with one same key/value but output the one has anotehr key/value higher

I'm trying to find a way to extract from an array of objects one specific object which has one value higher than another object.

The particularity is that I can have multiple objects and I need to find 2 or more which have the same key/value called role: GUARDIAN and then extract the one which has a higher value on the key `signOrder.

An array example

[
   {
     ....
     signOrder: 1,
     role: 'CONSENTEE',
   },
   {
     signOrder: 2,
     role: 'GUARDIAN',
   },
   {
     role: 'GUARDIAN',
     signOrder: 3
   }
 ]

Cases

  1. I'm getting an array-like above but there is only one guardian so I'm extracting only that guardian information

  2. I'm getting 2 guardians as above and need to extract the guardian with the highest number signOrder = 3

what will be the best way to do this in the right way to have the output of the 2 points above as

Output 1 I have only one guardian so I take that one

{
 role: 'GUARDIAN',
 signOrder: 2
}

Output 2 I have multiple Guardians so I take the one with the highest signOrder

{
 role: 'GUARDIAN',
 signOrder: 3
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const roles = [
 {
   signOrder: 1,
   role: 'CONSENTEE',
 },
 {
   signOrder: 2,
   role: 'GUARDIAN',
 },
 {
   role: 'GUARDIAN',
   signOrder: 3
 }
];
 
const getHighestByRole = (roles, roleToFind) => {
  // Get all objects for given role
  const foundRoles = roles.filter(({ role }) => role === roleToFind);
  // Sort them by `signOrder` then return first item
  return foundRoles.sort((a, b) => b.signOrder - a.signOrder)[0];
}

console.log(getHighestByRole(roles, 'GUARDIAN'));
console.log(getHighestByRole(roles, 'CONSENTEE'));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!