Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda