I have an array of objects as below
[
{
id: 10,
regionName: "Europa",
countryName: "Greece",
applicationName: "Ramco",
issueSummary: "No Reported Issues",
resolutionEta: "",
status: "UP"
},
{
id: 9,
regionName: "Asia PAC",
countryName: "Singapore",
applicationName: "Reckon",
issueSummary: "No Reported Issues",
resolutionEta: "",
status: "UP"
},
{
id: 7,
regionName: "Asia PAC",
countryName: "Thailand",
applicationName: "Javelin",
issueSummary: "No Reported Issues",
resolutionEta: "",
status: "UP"
},
{
id: 8,
regionName: "Europa",
countryName: "Greece",
applicationName: "Tamco",
issueSummary: "No Reported Issues",
resolutionEta: "",
status: "UP"
}
]
and I'm trying to group these objects in an array of regions, and each region should have an array of countries and each country should have an array of applications.
{
regions: [{
regionName: "Europe",
countries: [
{
countryName: "Greece",
applications: [
{
applicationName: "Ramco",
issueSummary: "No Reported Issues",
eta: "",
status: "UP",
},
{
applicationName: "Tamco",
issueSummary: "No Reported Issues",
eta: "",
status: "UP",
}
]
},
{
countryName: "France",
applications: [
{
applicationName: "Ramco",
issueSummary: "No Reported Issues",
eta: "",
status: "UP",
},
{
applicationName: "Tamco",
issueSummary: "No Reported Issues",
eta: "",
status: "UP",
}
]
},
]
.... and so on
},
here is what I have so far, it's working only for the regions array. I have a groupBy method and I am using it for regions and countries but this gives me separated arrays, not nested
ngOnInit(): void {
this.allRegions$ = this.dataService.getData();
this.groupData();
}
groupData() {
this.dataService.getData().subscribe(res => {
this.data = res;
const regions = this.groupBy('regions', this.data)
const countries = this.groupBy('countries', regions)
})
}
groupBy(key, array) {
return array.reduce((all, current) => {
const existingKey = all.find(existing => existing.key === current[key]);
console.log(existingKey)
if (!existingKey) {
all.push({key: current[key], values: [current]});
} else {
existingKey.values.push(current);
}
return all;
}, []);
}