I am new to Angular and RXJS - I'm trying to return normalized data from a rest api and assemble the hierarchy on the client. I created this stack blitz which is a really basic version of what I'm trying to accomplish: https://stackblitz.com/edit/angular-ffdbza
These interfaces ( ParentData, AssnData, and ChildData ) represent the JSON data being returned from the API. The Parent and Child interfaces are the way I want the data to be represented on the client (in the real app I will bind this new object to a hierarchical grid). The key points are that the Assn data has an attribute (statusCode) that needs to be applied to each Child based on the Parent.
// represents normalized data coming from the service
export interface ParentData {
parentCode: string,
name: string
}
export interface AssnData {
parentCode: string
childId: number,
statusCode: string
}
export interface ChildData {
childId: number,
type: string
}
// represents the merged data for display
export interface Parent {
parentCode: string,
name: string
kids: Child[]
}
export interface Child {
childId: number
type: string,
statusCode: string
}
This is the code I have so far (grabbed from data.component.ts in the stack blitz). It is adding the Assn objects to the correct parent but I'm having trouble merging the Child object with each Assn object. I am doing a console.log to see the results.
getRelationalData() {
let x = combineLatest(
this.parentData$,
this.assnData$,
this.childData$
).pipe(
map(([pData, aData, cData]) => {
return pData.map(p => {
return {
...p,
kids: aData.filter(a => a.parentCode === p.parentCode)
}
})
})
)
return x;
}