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

403
Views
Lodash groupBy() Method should be in same order as original array

I'm working on an array using groupBy lodash method, here the original array order does not exist on performing groupBy, is the any way to regain it after groupBy()

  const groupBy from "groupBy/lodash";

const mockData = [
 { groupId:"44", groupName:"mno",   },
 { groupId:"45", groupName:"pqr"  },
 { groupId:"40", groupName:"abc"  },
 { groupId:"41", groupName:"def"  },
]
const filteredArray = mockData.filter(item =>....); //logic to filter based on few parameters
const newArray = groupBy(filteredArray, "groupId");
console.log(newArray);

 result - {
 40: [{ groupId:"40", groupName:"abc"  }],
 41: [{ groupId:"41", groupName:"def"  }],
 44: [{ groupId:"44", groupName:"mno"  }],
 45: [{ groupId:"45", groupName:"pqr"  }],
}

expected - {
    44: [{ groupId:"44", groupName:"mno",   }],
    45: [{ groupId:"45", groupName:"pqr"  }],
    40: [{ groupId:"40", groupName:"abc"  }],
    41 [{ groupId:"41", groupName:"def"  }],
    }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

My guess, the groupBy method is sorting/grouping the array using groupId.

Can you clarify this question, are you saying the mockData order is diff and newArray order is diff. If so, then it is groupedBy groupId otherwise its mutating the array.

about 4 years ago · Juan Pablo Isaza Report

0

The _.groupBy() function creates an object. You are grouping by the id, and the ids are integers in a string form. ES6 traversal order rules order the integer properties by their value (ascending) - see this article. In your case, the groups are sorted by the id integer value.

Since _.groupBy() doesn't actually sort the keys, nor can you sort object's integer keys explicitly, you'll need to use another structure that preserves the order - a Map.

const groupByToMap = (arr, predicate) =>
  arr.reduce((acc, o) => {
    const key = predicate(o)
    
    if(!acc.has(key)) acc.set(key, [])
  
    acc.get(key).push(o)
  
    return acc
  }, new Map())

const mockData = [{ groupId:"44", groupName:"mno" }, { groupId:"45", groupName:"pqr"  },  { groupId:"40", groupName:"abc"  }, { groupId:"41", groupName:"def"  }]

const result = groupByToMap(mockData, o => o.groupId)

// !!! look at the browser's console (the snippet's would display an empty object) !!!
console.log(result) 

// when converting to an object the keys would be reordered by their integer value
console.log(Object.fromEntries(result)) 

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