-1

Im trying to bind value in Angular Material table, before that i need to process the GET response v

trying to achieve like below(just for understanding)

enter image description here

my faulty code

let filterByLocation = data.reduce((r, { group: location.country, ...object }) => {
    var finalArry = r.find(o => o.location === location);
    if (!finalArry) r.push(temp = { location, locationObj: [] });
    finalArry.locationObj.push(object);
    return r;
}, []);

console.log(filterByLocation);

thanks to @Nishant Dixit for his working snippet

const finalResponse = data.response.reduce((r, {
  location: {
    country: group
  },
  ...object
}) => {
  r[group] = r[group] || {
    location: group,
    locationObj: []
  };
  r[group].locationObj.push(object);
  return r;
}, {});

console.log(finalResponse)



const data = {
  "totalRec": 5,
  "response": [
  {
      "employee": {
        "uid": 1,
        "empName": "Jade"
      },
      "location": {
        "country": "UK",
        "subLocation": "London"
      },
      "department": {
        "sector": "IT"
      }
    },
    {
      "employee": {
        "uid": 2,
        "empName": "Mike"
      },
      "location": {
        "country": "UK",
        "subLocation": "Manchester"
      },
      "department": {
        "sector": "IT"
      }
    },
    {
      "employee": {
        "uid": 3,
        "empName": "Liya"
      },
      "location": {
        "country": "UK",
        "subLocation": "Southampton"
      },
      "department": {
        "sector": "HR"
      }
    },
    {
      "employee": {
        "uid": 3,
        "empName": "Brad"
      },
      "location": {
        "country": "USA",
        "subLocation": "Texas"
      },
      "department": {
        "sector": "IT"
      }
    },
    {
      "employee": {
        "uid": 3,
        "empName": "Brad"
      },
      "location": {
        "country": "USA",
        "subLocation": "Texas"
      },
      "department": {
        "sector": "NON-IT"
      }
    }
  ]
};

but the problem is i'm getting result like

UK : {
location : "UK"
....
}

in html, i don't want to explicitly mention UK with dot operation like below, instead row.location

<ng-container matColumnDef="facility">
<mat-header-cell *matHeaderCellDef mat-sort-header> Facility</mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.UK.location}} </mat-cell>
</ng-container>

could someone tell me how to convert this output like

location: {
country : 'UK'
}

or any random name like

obj: {
location: 'UK'
//rest of values for grouping
}

fiddle

Thanks to every one

Mr. Learner
  • 978
  • 2
  • 18
  • 48

1 Answers1

1

You can do something like this:

const countryMap = {};
data.response.forEach(item => {
   countryMap[item.location.country] = [ ...( countryMap[item.location.country] || [] ), item];
});

Now, this is how the countryMap will look like: enter image description here

Now, further to map it to the format you want, you can do this:

const mappedData = Object.entries(countryMap).map(entry => ({
   location: entry[0],
   response: entry[1]
}));

This will produce mappedData in this format:

enter image description here

Harsh Mittal
  • 326
  • 3
  • 7