Home > front end >  How to group an array of objects
How to group an array of objects

Time:02-06

I have an array of objects in the below format

const Employer = [{'company': 'ABC','location': 'Phase 1','year': '2012'}, 
    {'company': 'ABC','location': 'Phase2', 'year': '2013'}, 
    {'company': 'XYZ','location': 'Phase3','year': '2012'}];

And expected output is

  {
      'ABC':{
        'company': 'ABC',
         data:[
          {'location':'Phase1','year':2012},
          {'location':'Phase2', 'year':2013}]
         },
      'XYZ':{
        'company': 'ABC',
         data:[
          {'location':'Phase3','year':2012}]
      }           
    }

What I have tried is

name = 'Angular';
  groupedData:any;
  
  ngOnInit(){
    const Employer = [{'company': 'ABC','location': 'Phase 1','year': '2012'}, 
    {'company': 'ABC','location': 'Phase2', 'year': '2013'}, 
    {'company': 'XYZ','location': 'Phase3','year': '2012'}];
  
    this.groupedData = _.mapValues(_.groupBy(Employer, 'company'))    
    console.log(this.groupedData)
}
}

Output:
{
  "ABC": [
    {
      "company": "ABC",
      "location": "Phase 1",
      "year": "2012"
    },
    {
      "company": "ABC",
      "location": "Phase2",
      "year": "2013"
    }
  ],
  "XYZ": [
    {
      "company": "XYZ",
      "location": "Phase3",
      "year": "2012"
    }
  ]
}

Here again I need to group the data. Can anyone help me to get the expected output

CodePudding user response:

    const Employers = [{ 'company': 'ABC', 'location': 'Phase 1', 'year': '2012' },
    { 'company': 'ABC', 'location': 'Phase2', 'year': '2013' },
    { 'company': 'XYZ', 'location': 'Phase3', 'year': '2012' }];

    const output: any = {};
    for (const e of Employers) {
        if (output[e.company]) {
            output[e.company].data.push({ location: e.location, year: e.year });
        } else {
            output[e.company] = { company: e.company, data: [{ location: e.location, year: e.year }] };
        }
    }

    console.log(output);

CodePudding user response:

A very basic approach to do so is below

const Employers = [{'company': 'ABC','location': 'Phase 1','year': '2012'}, 
    {'company': 'ABC','location': 'Phase2', 'year': '2013'}, 
    {'company': 'XYZ','location': 'Phase3','year': '2012'}];
   
const result = {};

Employers.forEach(employer=>{
    const employerTemp = {...employer}
    delete employerTemp.company
    if(!result[employer.company]){
    result[employer.company] = {
         company: employer.company,
         data: [employerTemp]
      }
    } else {
         result[employer.company].data.push(employerTemp)
    }
})

console.log(result)

You can achieve the same thing using reduce also

CodePudding user response:

Edit: Fixed Answer

const Employer = [
  { company: "ABC", location: "Phase 1", year: "2012" },
  { company: "ABC", location: "Phase2", year: "2013" },
  { company: "XYZ", location: "Phase3", year: "2012" },
];

const groupedData = Employer.reduce((prev, current) => {
  if (!prev[current.company]) {
    prev[current.company] = { company: current.company, data: [] };
  }

  const currentTemp = { ...current };
  delete currentTemp.company;

  prev[current.company].data.push(currentTemp);
  return prev;
}, {});

console.dir(groupedData, { depth: null });

CodePudding user response:

const Employer = [{'company': 'ABC','location': 'Phase 1','year': '2012'}, 
    {'company': 'ABC','location': 'Phase2', 'year': '2013'}, 
    {'company': 'XYZ','location': 'Phase3','year': '2012'}];
    
    
 var obj = {};
 
 for(let a of Employer){
  if(obj[a.company]){
   obj[a.company].data.push({location:a.location,year:a.year})
  }else{
   obj[a.company] = {
    company:a.company,
    data:[{location:a.location,year:a.year}]
   }
  }
 }
 
 console.log(obj)

  •  Tags:  
  • Related