Javascript RXJS-绘制地图时返回可观察值
我有一个Javascript RXJS-绘制地图时返回可观察值,javascript,rxjs,rxjs-observables,Javascript,Rxjs,Rxjs Observables,我有一个inputIds数组,其中我正在执行一个返回名称和值对象的映射。在映射内部,我调用this.inputService.getInputFieldObject,它返回一个可观察的。如何返回订阅值而不是返回订阅值数组?因此,我可以将属性作为仅包含名称和值的数组返回 const attributes = inputIds.map((attributeName: string) => { // this.inputService.getInputFieldObject returns
inputIds
数组,其中我正在执行一个返回名称和值对象的映射。在映射内部,我调用this.inputService.getInputFieldObject
,它返回一个可观察的
。如何返回订阅值而不是返回订阅值数组?因此,我可以将属性作为仅包含名称和值的数组返回
const attributes = inputIds.map((attributeName: string) => {
// this.inputService.getInputFieldObject returns Observable<InputValue>
const inputSubscription = this.inputService.getInputFieldObject(attributeName).subscribe((val) => val.value)
return {
name: attributeName,
value: inputSubscription, // is there a getValue method to get value of subscription?
};
});
constattributes=inputIds.map((attributeName:string)=>{
//this.inputService.getInputFieldObject返回可观察的
const inputSubscription=this.inputService.getInputFieldObject(attributeName).Subscription((val)=>val.value)
返回{
姓名:attributeName,
value:inputSubscription,//是否有获取订阅值的getValue方法?
};
});
您可以将其包装在一个文件夹中并订阅,如下所示:
forkJoin(
inputIds.map((attributeName: string) =>
this.inputService.getInputFieldObject(attributeName).pipe(
map((inputValue: InputValue) => {
return {
name: attributeName,
value: inputValue
};
})
)
).subscribe(
(result: { name: string, value: InputValue }[]) => {
// do what you need to do with the result
},
(error) => {
// add code here if you need to handle failure on any of the calls
// to this.inputService.getInputFieldObject(), or any processing thereafter.
}
);
要解释代码的作用,请执行以下操作:
1。这将为inputIds
中的每个attributeName
调用inputService.getInputFieldObject()
。这将返回一个可观测值的数组
2。我们通过管道将对this.inputService.getInputFieldObject()的每个调用传递到一个映射,以返回attributeName和inputValue。因此,我们现在返回一个可观测值的数组
3.然后我们用a包装所有这些,然后订阅它forkJoin
可以接收一组可观察对象,并等待所有可观察对象完成。通过这样做,我们在处理结果之前等待所有可观测值返回其(最终)发射值。因此,您在subscribe()
中收到的值将是{name:string,value:InputValue}
的数组:
forkJoin(
....
).subscribe((result: { name: string, value: InputValue }[]) => {
// do what you need to do with the result
})
重要提示:
如果对inputService.getInputFieldObject()
的任何调用失败,将触发subcribe()
上的错误回调,而不是成功回调。您可以将其包装为并订阅,如下所示:
forkJoin(
inputIds.map((attributeName: string) =>
this.inputService.getInputFieldObject(attributeName).pipe(
map((inputValue: InputValue) => {
return {
name: attributeName,
value: inputValue
};
})
)
).subscribe(
(result: { name: string, value: InputValue }[]) => {
// do what you need to do with the result
},
(error) => {
// add code here if you need to handle failure on any of the calls
// to this.inputService.getInputFieldObject(), or any processing thereafter.
}
);
要解释代码的作用,请执行以下操作:
1。这将为inputIds
中的每个attributeName
调用inputService.getInputFieldObject()
。这将返回一个可观测值的数组
2。我们通过管道将对this.inputService.getInputFieldObject()的每个调用传递到一个映射,以返回attributeName和inputValue。因此,我们现在返回一个可观测值的数组
3.然后我们用a包装所有这些,然后订阅它forkJoin
可以接收一组可观察对象,并等待所有可观察对象完成。通过这样做,我们在处理结果之前等待所有可观测值返回其(最终)发射值。因此,您在subscribe()
中收到的值将是{name:string,value:InputValue}
的数组:
forkJoin(
....
).subscribe((result: { name: string, value: InputValue }[]) => {
// do what you need to do with the result
})
重要提示:
如果对inputService.getInputFieldObject()
的任何调用失败,将触发subcribe()
上的错误回调,而不是成功回调