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

99
Visualizações
Rearrange array of objects using object property Javascript

Is there a way to rearrange an Object or Objects in Firebase real time database? (Firebase saves it in a sorted manner by keys)

I couldnt figure it out so this is what I am doing on the client side

  1. Convert to array of Objects
  2. Sort the array by property value (look below)
  3. Change position (eg. from 4th to 1st) and update the required indexes
  4. Loop through the array and update index on all elements on firebase

My loop for step 3 is a little too complicated, is there an effecient, ES6 way to achieve the same?

Step 1: Data from firebase:

[
    id1: {
        name: 'name1',
        index: 3
    },
    id2: {
        name: 'name2',
        index: 0
    },
    id3: {
        name: 'name3',
        index: 1
    },
    id4: {
        name: 'name4',
        index: 2
    }
]

Step 2: Sort it

const sortedList = list.sort((a, b) =>
    a.val.index > b.val.index ? 1 : b.val.index > a.val.index ? -1 : 0
  )

output = [
    id2: {
        name: 'name2',
        index: 0
    },
    id3: {
        name: 'name3',
        index: 1
    },
    id4: {
        name: 'name4',
        index: 2
    }
    id1: {
        name: 'name1',
        index: 3
    },
]

Step 3: This is where I need help with a function to update the indexes when moved from one position to another

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