0

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

4 Answers 4

1
    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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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 });

2 Comments

nice but, this output she has already done you can read her question again
@Yirmin good job, but I did not vote down your answer, someone else did
-1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.