I have the following first array (let's call it organisation array):
[
{
"name": "Apple",
"id": "org1"
},
{
"name": "Microsoft",
"id": "org2"
}
]
and the second array is (let's call it unit array):
[
{
"organisation_id": "org1",
"id": "unitId1"
},
{
"organisation_id": "org30",
"id": "UnitId2"
}
]
And I want the output to look like this:
[
{
"name": "Apple",
"id": "org1",
"units": [
{
"organisation_id": "org1",
"id": "unitId1"
}
]
},
{
"name": "Microsoft",
"id": "org2",
"units": []
}
]
And I want to combine these two arrays (per object) into one array of objects, and create a units property when id === organisation_id. This works when I do something like this:
const combOrgUnit: OrganisationAndUnit[] = [];
this.organisations.subscribe(orgList => {
this.units.subscribe(unitList => {
for (const org of orgList) {
const tempComb: OrganisationAndUnit = {organisation: undefined, units: []};
const tempUnit: Unit[] = [];
tempComb.organisation = org;
for (const unit of unitList) {
if (org.id === unit.organisation_id) {
tempUnit.push(unit);
tempComb.units = tempUnit;
}
}
combOrgUnit.push(tempComb);
}
console.log(combOrgUnit);
});
});
This looks like a sloppy way to check for conditions, is there more rxjs way of doing this? I know that there is one using combineLatest and forkJoin both seems to be deprecated.
Any help would be appreciated.