如何使用angular将值数组传递给api调用,并将响应存储在单个数组中



我有一个数组值,将每个数组值传递给api调用和我需要知道如何将所有响应保存在angular

的单个数组中

this.result:[]=[];
this.formData=["S1", "S2"]
ngOnInit() {
for(let item of formData) {
this.httpClient.post(this.PartsAPIURL, item, { headers: headers })
.subscribe((data: any) => {
this.result.push(...data);
});
}
}
console.log(result);

Expected Result;
Response all should be stored in single array.

forkJoin将发出多个请求并发出包含所有响应的单个数组:

public result: [] = [];
public formData = ['S1', 'S2'];
private requests = this.formData.map(
item => this.httpClient.post(this.PartsAPIURL, item, { headers })
);
private results$ = forkJoin(this.requests);
ngOnInit() {
this.results$.subscribe(
result => this.result = result
);
}