Angular 函数数的可观测性 f1(){ //API调用 console.log('f1'); } f2(){ console.log('f2'); 对于(i=0;i this.getHTTP.getData(url))) .订阅((结果)=>{ this.resultArray.push(结果); log('this.resultaray',this.resultaray'); }); }

Angular 函数数的可观测性 f1(){ //API调用 console.log('f1'); } f2(){ console.log('f2'); 对于(i=0;i this.getHTTP.getData(url))) .订阅((结果)=>{ this.resultArray.push(结果); log('this.resultaray',this.resultaray'); }); },angular,angular2-observables,Angular,Angular2 Observables,这是我在API调用中使用的函数 现在我想使用for循环并使用这个数据this.resultArray。 像这样 apiParam: any = ['posts', 'albums', 'comments', 'photos', 'users']; getObs() { const apiRoot = 'https://jsonplaceholder.typicode.com/'; const urls = []; for (let i = 0; i < this.apiParam.

这是我在API调用中使用的函数 现在我想使用for循环并使用这个数据this.resultArray。 像这样

  apiParam: any = ['posts', 'albums', 'comments', 'photos', 'users'];

 getObs() {
const apiRoot = 'https://jsonplaceholder.typicode.com/';
const urls = [];
for (let i = 0; i < this.apiParam.length; i++) {
  urls.push(apiRoot + this.apiParam[i]);
  // const url = apiRoot + this.apiParam[i];
}
of(...urls).pipe(
  concatMap((url: string) => this.getHTTP.getData(url)))
  .subscribe((result) => {
    this.resultArray.push(result);
    console.log('this.resultArray', this.resultArray);
  });
}
for(让index=0;index
但是,每当我这样做的时候,for循环是在得到最终结果之前首先运行的。
请帮帮我

您可以简单地将第一个调用链接到第二个调用,就像这样(在组件内部):


查看我的编辑。此。第一个在内运行5次。因此,如果我立即写入此.second,则此.second将不会使用完整的数据。它只是与可观察物有关。我无法创建一个。所以请帮我创建一个可观察的
  apiParam: any = ['posts', 'albums', 'comments', 'photos', 'users'];

 getObs() {
const apiRoot = 'https://jsonplaceholder.typicode.com/';
const urls = [];
for (let i = 0; i < this.apiParam.length; i++) {
  urls.push(apiRoot + this.apiParam[i]);
  // const url = apiRoot + this.apiParam[i];
}
of(...urls).pipe(
  concatMap((url: string) => this.getHTTP.getData(url)))
  .subscribe((result) => {
    this.resultArray.push(result);
    console.log('this.resultArray', this.resultArray);
  });
}
for (let index = 0; index < this.resultArray.length; index++) {
 console.log(this.apiParam[index]);
  this.finalObject = { [this.apiParam[index]]: this.resultArray[index] 
};
  this.finalArray.push(this.finalObject);
 }
 console.log('========this.finalArray============================');
 console.log(this.finalArray);
 console.log('====================================');
constructor(private http: HttpClient) {}

first() {
  console.log('API called!');
}

second(data) {
  for (let item of data) {
    ...
  }
}

apiCall() {
  this.http.get(someURL).subscribe(
    (data) => {
      this.first();
      this.second(data);
    }
  );
}