這是兩個輸出
"statusCode": 200,
"data": [
{
"color": {
"id": "1111",
"name": null,
"hex": null,
"image": "\/img\/.jpg",
"subColors": [
{
"id": null,
"name": "White",
"hex": "#F5F4F8",
"image": null,
"subColors": []
},
{
"id": null,
"name": "Grey",
"hex": "#6c6f70",
"image": null,
"subColors": []
}
]
},
"articleVariants": [
{
"id": "941282",
"articleNumber": "1000204445",
"size": "68\/74",
]
},
和
{
"statusCode": 200,
"data": [
{
"article": 1000204445,
"ownStock": 3,
"supplierStock": 18,
"notice": null,
"nextAvailability": null,
"hasMoreOwnStock": false,
"ownStockText": "3"
}
]
}
我需要根據 articleVariants.articleNumber 中的相同值和第二次呼叫中的 article 值合并這兩個回應。
第一的
getArticleVariantsByColor(masterArticleId: string, colors: object, sizes: object, language: Language): Observable<ApiResponse> {
let a = {
colors,
sizes,
};
return this.http.post<ApiResponse>(
environment.masterServiceUrl '/variants/service/' language.isoCode '/' masterArticleId, a);
}
第二
getStock(article: any): Observable<ApiResponse> {
return this.http.post<ApiResponse>(
'https://shopapi/stocks/article/', article)
;}
這些是我在組件頁面上撰寫的功能。
第一次通話
getArticleVariantsByColor() {
if (this.masterArticle) {
const subscription: Subscription = this.articleVariantService.getArticleVariantsByColor(
this.masterArticle.id,
{},
{},
this.language,
).subscribe(apiResponse => {
this.allArticleVariantsByColor = apiResponse.data as ArticleVariantsByColor[];
this.allArticleVariantsByColor.forEach(variantColor => {
variantColor.allArticleVariants = variantColor.articleVariants;
});
this.articleVariantsByColor = this.allArticleVariantsByColor;
this.getStock();
},
);
this.subscriptions.push(subscription);
}
}
第二次通話
getStock() {
const articleNumbers = this.allArticleVariantsByColor.flatMap(v => {return v.articleVariants}).map(v=> {return Number ( v.articleNumber)});
const subscription: Subscription = this.stockService.getStock(
articleNumbers,
).subscribe(apiResponse => {
this._stockService = apiResponse.data as StockService[];
if (this.masterArticle.stocks.article === this.articleNumbers) {
return this.availableStocks.map(item => item.map(v=>v.article === articleNumbers) );
}
},
);
this.subscriptions.push(subscription);
}
這應該展平文章編號的陣列,stockServices 應該訂閱這些值并將它們與請求的文章編號進行比較。如果它們相等,則應該回傳值并將它們映射到請求。
可悲的是我在這里有點迷路,也許有人可以幫助我?如何合并這些具有相同值的請求并將新回應映射到另一個。
uj5u.com熱心網友回復:
尤其是在 Angular 中作業時,您可以(并且可能應該)廣泛使用ngrx來處理此類事情。
對于您的具體問題,您可以使用combineLatest。大致如下:
combineLatest(getStock(...), getArticleVariantsByColor(...)).subscribe(
[stockResult, articleVariantsResult] => {
/* Now you can merge the results here and do smth with them
like assigning to a variable of the component */
const mergedItems = stockResult.data.map(stock =>
articleVariantsResult.data.find(variant =>
variant.articleVariants.include(articleVariant =>
articleVariant.articleNumber === stock.article
)
)
)
}
)
我假設我的合并代碼并不能完全滿足您的需求,我只是想演示combineLatest以及如何訪問 api 結果。讓我知道你是否成功
uj5u.com熱心網友回復:
讓我們將解決方案分為兩個步驟。
沒有 Observables 的第一步,讓我們組合回應。
const response1 = {"statusCode": 200,
"data": [
{
"color": {
"id": "1111",
"name": null,
"hex": null,
"image": "\/img\/.jpg",
"subColors": [
{
"id": null,
"name": "White",
"hex": "#F5F4F8",
"image": null,
"subColors": []
},
{
"id": null,
"name": "Grey",
"hex": "#6c6f70",
"image": null,
"subColors": []
}
]
},
"articleVariants": [
{
"id": "941282",
"articleNumber": "1000204445",
"size": "68\/74",
},
]
}
]
};
const response2 = {
"statusCode": 200,
"data": [
{
"article": 1000204445,
"ownStock": 3,
"supplierStock": 18,
"notice": null,
"nextAvailability": null,
"hasMoreOwnStock": false,
"ownStockText": "3"
}]};
var dataArray1 = response1.data;
var dataArray2 = response2.data;
const mergedResponse = dataArray2.map(d2 => ({...d2,
...dataArray1.find(d1 => d1.articleVariants.find(av=> av.articleNumber
=== d2.article))}));
console.log(mergedResponse);
第二步——forkJoin 使用來處理 Observables。
const postResponse1 = of(response1);
const postResponse2 = of(response2);
forkJoin({
response2: postResponse2,
response1: postResponse1
})
.subscribe(({response2, response1}) => {
var dataArray1 = response1.data;
var dataArray2 = response2.data;
const mergedResponse = dataArray2.map(d2 => ({...d2,
...dataArray1.find(d1 =>
d1.articleVariants.find(av=> av.articleNumber === d2.article))}));
console.log(mergedResponse);
});
uj5u.com熱心網友回復:
你可以使用forkJoin(),我希望它對你有用。
getData() {
let requestArray = [this.articleVariantService.getArticleVariantsByColor(this.masterArticle.id,{},{}, this.language), this.stockService.getStock(articleNumbers)]
forkJoin(requestArray).subscribe(results => {
console.log(results[0], result[1]);
let response = results;
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/467451.html
上一篇:使用AWSSDK(S3.putObject)將可讀流上傳到S3(node.js)
下一篇:添加兩個$post->變數
