简体   繁体   中英

How to map new array of objects out of observable?

I have a response from HttpClient in the form of a JSON object. In my observable, I am trying to use RXJS methods to get the data I need.

The data I need is an array of objects on the response object. I use pluck to get the array out of the response object and that works fine. However, when I try to map the result, I am getting the error Property map does not exist on type {} . I am trying to map the array of objects to another array of objects.

Here is the core of the observable piping:

.pipe(
    switchMap(data => this._settingsService.getSortPlan(data.plan, data.sorter)),
    pluck('sortPlanRoutes'),
    map(planRoutes => planRoutes.map(this.mapSortPlans))
)

And the map function:

private mapSortPlans(plan) {
    return {
        route: plan.sortDestination.sortDestination,
        locationType: plan.physicalLocationAttribute.attrValue,
        destination: plan.physicalLocation.locationAlias,
        priority: plan.priority
    };
}

I expect the map(planRoutes => planRoutes.map(this.mapSortPlans)) to be able to run the mapSortPlans function, but it's giving me the error Property map does not exist on type {} .

Not sure if this is the right way, but I was able to cast planRoutes to an array:

switchMap(data => this._settingsService.getSortPlan(data.plan, data.sorter)),
pluck('sortPlanRoutes'),
map(planRoutes => (planRoutes as Array<any>).map(this.mapSortPlans))

That got rid of the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM