我如何订阅 Observable 并在 Javascript 对象中使用该值?
How do I subscribe to an Observable and use that value in a Javascript Object?
总的来说,我对 Observables、RxJS 和 Angular 还很陌生。我正在使用 Angular 7 (Ionic 4),我很难想出以下问题的解决方案。
在我的应用程序中,我发出如下 HTTP GET 请求:
myData = this.http.get("https://example.com/api").pipe(
map(results => {
return results["payload"]
})
)
这个 HTTP GET 请求 returns 一个具有以下数据的 Observable myData
:
const data = [
{
"id": 1,
"name": "abc",
"description": "test1"
},
{
"id": 2,
"name": "def",
"description": "test2"
},
...
...
]
我想向这个数组中的每个对象添加另一个键 color
,如下所示:
const data = [
{
"id": 1,
"name": "abc",
"description": "test1",
"color": "green"
},
{
"id": 2,
"name": "def",
"description": "test2",
"color": "red"
},
...
...
]
我不想为每个对象硬编码 color
的值,而是想从另一个名为 colorService
的服务中的函数 getColor(id)
中检索此键值。
问题是 colorService.getColor(id)
returns 一个 Observable。
问题:如何为数组中的每个对象订阅 colorService.getColor(id)
?
我想做这样的事情:
const data = [
{
"id": 1,
"name": "abc",
"description": "test1",
"color": <subscribe to colorService.getColor(1)>
},
{
"id": 2,
"name": "def",
"description": "test2",
"color": <subscribe to colorService.getColor(2)>
},
...
...
}
希望我说清楚了。我的概念在这一点上相当薄弱,所以如果其中一些听起来令人困惑,我深表歉意。
这就是你能做的[见代码注释中的解释]-
myData = this.http.get("https://example.com/api")
.pipe(
mergeMap(results => {
const data = results["payload"];
//I am assuming that `data` is the array that you mentioned in your question
//Iterate through each of the object in the array and map it to an observable which
//will fetch the color and then return the data with color
//obs will be an array of observables for each data
const obs = data.map(d => {
return this.colorService.getColor(d.id)
.pipe(
map(color => {
return {...d, color};
})
);
});
//wait for all the obs to be returned;
return forkJoin(obs);
}),
tap(dataArrayWithColor => {
//dataArrayWithColor will have an array of object which will have data with color
console.log(dataArrayWithColor);
})
);
希望对您有所帮助。
总的来说,我对 Observables、RxJS 和 Angular 还很陌生。我正在使用 Angular 7 (Ionic 4),我很难想出以下问题的解决方案。
在我的应用程序中,我发出如下 HTTP GET 请求:
myData = this.http.get("https://example.com/api").pipe(
map(results => {
return results["payload"]
})
)
这个 HTTP GET 请求 returns 一个具有以下数据的 Observable myData
:
const data = [
{
"id": 1,
"name": "abc",
"description": "test1"
},
{
"id": 2,
"name": "def",
"description": "test2"
},
...
...
]
我想向这个数组中的每个对象添加另一个键 color
,如下所示:
const data = [
{
"id": 1,
"name": "abc",
"description": "test1",
"color": "green"
},
{
"id": 2,
"name": "def",
"description": "test2",
"color": "red"
},
...
...
]
我不想为每个对象硬编码 color
的值,而是想从另一个名为 colorService
的服务中的函数 getColor(id)
中检索此键值。
问题是 colorService.getColor(id)
returns 一个 Observable。
问题:如何为数组中的每个对象订阅 colorService.getColor(id)
?
我想做这样的事情:
const data = [
{
"id": 1,
"name": "abc",
"description": "test1",
"color": <subscribe to colorService.getColor(1)>
},
{
"id": 2,
"name": "def",
"description": "test2",
"color": <subscribe to colorService.getColor(2)>
},
...
...
}
希望我说清楚了。我的概念在这一点上相当薄弱,所以如果其中一些听起来令人困惑,我深表歉意。
这就是你能做的[见代码注释中的解释]-
myData = this.http.get("https://example.com/api")
.pipe(
mergeMap(results => {
const data = results["payload"];
//I am assuming that `data` is the array that you mentioned in your question
//Iterate through each of the object in the array and map it to an observable which
//will fetch the color and then return the data with color
//obs will be an array of observables for each data
const obs = data.map(d => {
return this.colorService.getColor(d.id)
.pipe(
map(color => {
return {...d, color};
})
);
});
//wait for all the obs to be returned;
return forkJoin(obs);
}),
tap(dataArrayWithColor => {
//dataArrayWithColor will have an array of object which will have data with color
console.log(dataArrayWithColor);
})
);
希望对您有所帮助。