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

155
Views
Grouping an array of objects and assign new combined value to each object

From an array of objects, I would like to add an alumni attribute for each object. The value of this attribute is a concatenation separated by "-" of the names of all the objects that have the same school value.

Now the tricky part is that I need to make it performance efficient as possible, as the initial object array is very large.

I figured out a solution using groupBy school, in each group map the different names of and concat them into a string, then looping on each object to add the new attribute and value, and finally loop on each group to place all the objects in a new array (do undo the groups).

I am certain that there is a much cleaner way to do this. Any suggestion is more than welcome.

Below is the initial array, and the expected result with the new attribute added to each object in the array.

var initialArr = [
  {name:"A", school:"LFM"},
  {name:"B", school:"LFM"},
  {name:"C", school:"PBE"},
  {name:"D", school:"LFM"},
  {name:"E", school:"BPE"},
  {name:"F", school:"LFM"}
];

var expectedResult = [
  {name:"A", school:"LFM", alumni:"A-B-D-F"},
  {name:"B", school:"LFM", alumni:"A-B-D-F"},
  {name:"C", school:"PBE", alumni:"C-E"},
  {name:"D", school:"LFM", alumni:"A-B-D-F"},
  {name:"E", school:"BPE", alumni:"C-E"},
  {name:"F", school:"LFM", alumni:"A-B-D-F"}
];
about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

I'd say the most efficient way to do it would be to create an object which maps school to an array of names. Then map the initial array:

var initialArr= [
  {name:"A", school:"LFM"},
  {name:"B", school:"LFM"},
  {name:"C", school:"PBE"},
  {name:"D", school:"LFM"},
  {name:"E", school:"BPE"},
  {name:"F", school:"LFM"}
];

var schoolObj = initialArr.reduce((a, { name, school }) => {
  a[school] = (a[school] || []);
  a[school].push(name);
  return a;
}, {});

var expectedResult = initialArr.map(e => {
  e.alumni = schoolObj[e.school].join("-");
  return e;
});

console.log(expectedResult);

This is O(n) time complexity but also O(n) space complexity (I think - still learning in that regard). You could make a more inefficient O(n^2) solution that had constant space but since you mentioned the input array to be very large, I assumed time would be your largest constraint.

Note that you may have to perform a sort on the array of alumni, in which case the time complexity will likely be O(n log n) if you use an efficient sort (I leave the choice of algorithm to you).

about 4 years ago · Santiago Gelvez Report

0

Filter and map the data to your liking.

    const initialArr= [
      {name:"A", school:"LFM"},
      {name:"B", school:"LFM"},
      {name:"C", school:"PBE"},
      {name:"D", school:"LFM"},
      {name:"E", school:"BPE"},
      {name:"F", school:"LFM"}
    ]

    console.log(
      initialArr.map(x => ({
        name: x.name, 
        school: x.school, 
        alumni: `${x.name}-${initialArr.filter(y => x.school === y.school && y.name !== x.name).map(y => y.name).join("-")}`
      }))
    )

about 4 years ago · Santiago Gelvez Report

0

The way how you describe it is efficient.

It depends only on how exactly you do the groupBy and other parts. If there is no cycle-in-cycle (and there has not to be), the complexity is O(n) (linear), which is as good as you can get. There can be space for some microoptimization but that is something that is not usually very effective and in most projects not worth the time.

about 4 years ago · Santiago Gelvez 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!