Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!