如何将新的对象数组映射为不可观察?

时间:2019-01-21 21:42:36

标签: javascript angular typescript rxjs

我从HttpClient收到一个JSON对象形式的响应。可以观察到,我正在尝试使用RXJS方法来获取所需的数据。

我需要的数据是响应对象上的对象数组。我使用pluck将数组从响应对象中取出,效果很好。但是,当我尝试映射结果时,出现错误Property map does not exist on type {}。我正在尝试将对象数组映射到另一个对象数组。

这是可观察管道的核心:

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

和地图功能:

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

我希望map(planRoutes => planRoutes.map(this.mapSortPlans))能够运行mapSortPlans函数,但这会给我错误Property map does not exist on type {}

1 个答案:

答案 0 :(得分:0)

不知道这是否正确,但是我能够将planRoutes转换为数组:

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

摆脱了错误。