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

124
Visualizações
Sort object keys by the number of elements in the array stored in each in JavaScript

I have a project where I receive a list of products which I then group by the manufacturer. I want to programmatically display these on the page, but need to do so in the order of most to least products. I need to come up with an algorithm to sort the object keys from most products to least products.

Here is a simplified example of what I have, and where I need to get to:

let devices = {
  Sony: [
    {name: 'Experia 1', productCode: 's1'}, 
    {name: 'Experia 2', productCode: 's2'}
  ],
  Apple: [
    {name: 'iPhone 8', productCode: 'a1'}, 
    {name: 'iPhone 9', productCode: 'a2'}, 
    {name: 'iPhone 10', productCode: 'a3'}, 
    {name: 'iPhone 11', productCode: 'a4'}
  ],
  Huawei : [
    {name: 'S 21', productCode: 'h1'}
  ],
  LG: [
    {name: 'V60', productCode: 'l1'},
    {name: 'V60 Ultra', productCode: 'l2'},
    {name: 'G7', productCode: 'l3'}
  ]
}

I need to order the data so it starts with the manufacturer with the most is first then descends to the manufacturer with the least. So it needs to be modified to look like this:

{
  Apple: [
    {name: 'iPhone 8', productCode: 'a1'}, 
    {name: 'iPhone 9', productCode: 'a2'}, 
    {name: 'iPhone 10', productCode: 'a3'}, 
    {name: 'iPhone 11', productCode: 'a4'}
  ],
  LG: [
    {name: 'V60', productCode: 'l1'},
    {name: 'V60 Ultra', productCode: 'l2'},
    {name: 'G7', productCode: 'l3'}
  ]
  Sony: [
    {name: 'Experia 1', productCode: 's1'}, 
    {name: 'Experia 2', productCode: 's2'}
  ],
  Huawei : [
    {name: 'S 21', productCode: 'h1'}
  ],
}

The devices on offer will differ frequently, so I need to sort this data every time the user accesses the page. Because of that I'm looking for the shortest possible syntax to achieve this.

I am finding tutorials on how to sort object keys alphabetically, and other similarly related algorithms, but not one that does exactly this. I've started trying to customize some basic sorting algorithms, but data manipulation is not my strong suit, so they are either failing or turning into a bloated spaghetti code mess.

Is there a short and easy way to convert the data as described? Perhaps there is a sorting method or design pattern that is particularly well suited to the task. I've been looking into lodash commands, but am not finding anything that immediately jumps out as a solution.

I'd be very grateful for any advice that anyone could offer. Thank you in advance

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

0

You can convert it to an array, sort it by the length, and convert it back into an object.

let devices = {
  Sony: [
    {name: 'Experia 1', productCode: 's1'}, 
    {name: 'Experia 2', productCode: 's2'}
  ],
  Apple: [
    {name: 'iPhone 8', productCode: 'a1'}, 
    {name: 'iPhone 9', productCode: 'a2'}, 
    {name: 'iPhone 10', productCode: 'a3'}, 
    {name: 'iPhone 11', productCode: 'a4'}
  ],
  Huawei : [
    {name: 'S 21', productCode: 'h1'}
  ],
  LG: [
    {name: 'V60', productCode: 'l1'},
    {name: 'V60 Ultra', productCode: 'l2'},
    {name: 'G7', productCode: 'l3'}
  ]
}

devices = Object.fromEntries(Object.entries(devices).sort( (a,b) => b[1].length - a[1].length));

console.log(devices)

If you are looping over the object, I would not convert it back to an object, I would just use Object.entries and output my data based on that.

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