Extracting nested array from an parent observable array in 1 emission

222 Views Asked by At

Need some help with RXJS extraction and merging of data from observable.

[
    collection: {
        "id": "",
        "purpose": "",
        "balance": "",
        "creator": "",
        "dateCreated": "",
        "dateUpdated": "",
        "userId": "",
        "schedule": [
            {
                "id": "",
                "amount": "",
                "status": "",
                "type": "",
                "dateCreated": "",
                "dateUpdated": ""
            },
            {
                "id": "",
                "amount": "",
                "status": "",
                "type": "",
                "dateCreated": "",
                "dateUpdated": ""
            },
            .....
        ]
    }
]

How do I extract all the schedule objects within each Collection parent object and combine them together as a array of Schedules? I tried using mergeMap with map. As well as forkjoin and combinedLatest. However, I still couldn't figure out the appropriate method.

1

There are 1 best solutions below

0
Eliseo On

It's only use "map". Think rxjs map operator like map an array. The map can be as complex you want

this.service.getData().pipe(map((res:any[])=>{
   const schedules=[]
   res.forEach(x=>{
     schedules=[...schedules,...x.shedule] //using spread operator

     //or use concat
     //schedules=schedules.concat(x.shedule)

     //or using splice
     //schedules.splice(schedules.length-1,0,x.shedule)

     //or what-ever
   }
   return schedules;
}))