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

188
Visualizações
Sort an array based on another array which may be incomplete

Let's say I have a bunch of SKUs which look something like this:

{ id: '1234567', size: 'L', available: true, color: { id: 1234 }}
{ id: '1234588', size: 'XS', available: true,  color: { id: 1234 }}
{ id: '1234568', size: 'M', available: true, color: { id: 1234 }}
{ id: '1234569', size: 'XL', available: true,  color: { id: 1234 }}
{ id: '1234573', size: 'S', available: true,  color: { id: 1234 }}

I also have an source of truth array that looks something like this:

{ "1234": ["XS", "S", "M", "L", "XL", "XXL"] }

The app is currently mapping the initial array of objects in order. As you can see, each SKU has the color identifier which is the key for the array of truth.

Normally this would be pretty simple to re-organize. Here's the issue:

The source of truth only returns available items. So, supposing that the inventory for "1234" was out of stock on everything except "XXL" the response would be ["XXL"]. I need all the other entries (items in the SKU) to show up, just be crossed out as sold out. But they all need to be in order.

In other words, if I receive the ["S", "M"] as the correct order, the result sould look like this:

{ id: '1234588', size: 'XS', available: true,  color: { id: 1234 }}
{ id: '1234573', size: 'S', available: true,  color: { id: 1234 }}
{ id: '1234568', size: 'M', available: true, color: { id: 1234 }}
{ id: '1234567', size: 'L', available: true, color: { id: 1234 }}
{ id: '1234569', size: 'XL', available: true,  color: { id: 1234 }}

It seems doable but the longer I look at it the less possible it seems. Keep in mind also that the size isn't strictly limited to letters but can be alphanumeric (30D) or just numeric (43) or they could be words - "Full-size" so regular ole .sort() won't do.

How do I use the source of truth array to rearrange the array of SKUs to be in accordance with the order provided by it?

about 4 years ago · Juan Pablo Isaza
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