Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
I want to change output of API with rxjs

I have API call, where I use pipe

getCountriesListNights(){
    const r = this.http.get<any>(this.APIUrl + "/WorldMapDataNights?&year=" + this.selectedYear + "&month=" + this.selectedMonth +"&gender=" + this.selectedGender + "&age="+this.selectedAge + "&quarter=" + this.selectedQuarter)
      .pipe(map((res) => {return res.map((res: { id: any; countryName: any; value: any; year: any; name: string; multiPolygon: any[] }) =>({countryName: res.countryName,  id: res.id,  value: res.value, year: res.year, name: res.name, multiPolygon: res.multiPolygon  }))}))
      .subscribe(res =>{
        this.renderMapObject(res);
    });
  };

It works good, I can change the output, but I want to divide value by other API's value, when country name's are same in both API's. (I want to divide res.value by otherapi.value.)

what can I do now?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Something like this would do the trick

getCountriesListNights(){
  const r = this.http.get<any>(this.APIUrl + "/WorldMapDataNights?&year=" + this.selectedYear + "&month=" + this.selectedMonth +"&gender=" + this.selectedGender + "&age="+this.selectedAge + "&quarter=" + this.selectedQuarter).pipe(map((res) => {return res.map((res: { id: any; countryName: any; value: any; year: any; name: string; multiPolygon: any[] }) =>({countryName: res.countryName,  id: res.id,  value: res.value, year: res.year, name: res.name, multiPolygon: res.multiPolygon  }))})).subscribe(res =>{
    console.log(res);
    this.renderMapObject(res);
    this.http.get<any>(this.APIUrl + "/yourSecondApiURL").subscribe(otherapiRes =>{
      const finalResult = res.value / otherapiRes .value;
      console.log(finalResult);      
    }
  });
};

it is not pretty and you could use something like

const res = await this.http.get<any>("...firstApiUrlAndMapping...").toPromise();
const res2 = await this.http.get<any>("...secondApiUrlandMapping...").toPromise();
finalResult = res.value/res2.value;
 

but both ways should work just fine.

EDIT

A basic example of running two http calls inside each other would be

const res = this.http.get<any>('https://reqbin.com/echo/get/json').subscribe(res =>{
  console.log(res);
  this.http.get<any>('https://reqbin.com/echo/get/json').subscribe(otherapiRes =>{
    console.log(otherapiRes);
    console.log('Both Results:' + res.success + ' -- ' + otherapiRes.success);
  });
});
about 4 years ago · Santiago Trujillo Report

0

Okay, to achieve the aim, you need a fulfilled response from two sources. In general, the Promise.all() is giving what you need.

There are several ways to achieve your need in RxJS world. For example, the operator zipWith returns a stream that notifies of an array of results (its source and its argument inputs).

A sample you can play with:

of('Foo')
  .pipe(
    delay(1000),
    tap(() => console.log('Foo succeeded')),
    zipWith(
      of('Bar').pipe(
        delay(2000),
        tap(() => console.log('Bar succeeded'))
      ),
      of('Baz').pipe(
        delay(2500),
        tap(() => console.log('Baz succeeded'))
      )
    ),
    // You can process and transform the outcome
    map(([foo, bar, baz]) => foo + bar + baz)
  )
  .subscribe(result => {
    console.log('All succeeded:', result);
  });

https://stackblitz.com/edit/rxjs-er8zx1?file=index.ts

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!