Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
Javascript - find highest value in an object of arrays of objects

I have an object containing arrays of objects. I'm trying to find the highest value of an object property, 'sortOrder' without manually iterating through the arrays and objects.

So my variable looks like this following:

const myObj = {
 people: [
   0: {firstname: 'Dave', lastName: 'Jones', sortOrder: 22},
   1: {firstname: 'Jane', lastName: 'Smith', sortOrder: 11}
 ],
 otherPeople: [
   0: {firstname: 'Jen', lastName: 'SomeLastName', sortOrder: 33},
   1: {firstname: 'ExampleFirstName', lastName: 'ExampleLastName', sortOrder: 12}
 ]
};

So I'd be trying to iterate through this to eventually find, in this case, the highest sortOrder of 33. Not necessarily the array index or the object containing it, just the number.

Thanks

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

(I had to slightly correct your input data: arrays don't include the index number as you're showing.)

Easiest to combine the two arrays in the object into one list and then use reduce to find the maximum:

const myObj = {
  people: [
    {
      firstname: 'Dave',
      lastName: 'Jones',
      sortOrder: 22
    },
    {
      firstname: 'Jane',
      lastName: 'Smith',
      sortOrder: 11
    }
  ],
  otherPeople: [
    {
      firstname: 'Jen',
      lastName: 'SomeLastName',
      sortOrder: 33
    },
    {
      firstname: 'ExampleFirstName',
      lastName: 'ExampleLastName',
      sortOrder: 12
    }
  ]
};


let allPeople = [...myObj.people, ...myObj.otherPeople];

let max = allPeople.reduce((maxSort, person) => {
  return Math.max(maxSort, person.sortOrder)
}, 0));

console.log(max)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can order them by the highest sortOrder and then take the higher of the highest elements in each array:

const myObj = {
  people: [{
      firstname: 'Dave',
      lastName: 'Jones',
      sortOrder: 22
    },
    {
      firstname: 'Jane',
      lastName: 'Smith',
      sortOrder: 11
    }
  ],
  otherPeople: [{
      firstname: 'Jen',
      lastName: 'SomeLastName',
      sortOrder: 33
    },
    {
      firstname: 'ExampleFirstName',
      lastName: 'ExampleLastName',
      sortOrder: 12
    }
  ]
};
Object.values(myObj).forEach(peopleArray => {
  peopleArray.sort((a, b) => b.sortOrder - a.sortOrder)
})
let maxValue = Math.max(myObj.people[0].sortOrder, myObj.otherPeople[0].sortOrder)
console.log(maxValue)

about 4 years ago · Juan Pablo Isaza Relatório

0

  • Using Object#values get the list of arrays
  • Using Array#flat, convert the 2d array into one
  • Using Array#reduce, iterate over this list while updating a max

const myObj = {
  people: [ {firstname: 'Dave', lastName: 'Jones', sortOrder: 22}, {firstname: 'Jane', lastName: 'Smith', sortOrder: 11} ],
  otherPeople: [ {firstname: 'Jen', lastName: 'SomeLastName', sortOrder: 33}, {firstname: 'ExampleFirstName', lastName: 'ExampleLastName', sortOrder: 12} ]
};

const maxSortOrder = 
  Object.values(myObj)
  .flat()
  .reduce((max, { sortOrder = 0 }) => sortOrder > max ? sortOrder : max, 0);

console.log(maxSortOrder);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda