0

here i got two Json responses ie., books and authors.i want those two responses in a single array

this.http.getbooks().subscribe(
    data=>{
    this.getbooksinfo=data;
    }
)
this.http.getauthors().subscribe(
    data=>{
    this.authors=data;
    }

)

getauthors,getbooks info are two methods that gets info from restapi using http get method..how to store this.author and this.booksinfo in single array?

2
  • Let newData = this.getbooksinfo.concat(this.authors). Commented Jun 6, 2018 at 14:22
  • thats not working,i already tried it :(, Commented Jun 6, 2018 at 16:09

2 Answers 2

2

You can optionally use forkJoin:

const books = this.http.getbooks();
const authors = this.http.getauthors();

forkJoin([books, authors]).subscribe(response => {
 // response[0] will be req1 response
 // response[1] will be req2 response
})

The other option as mentioned in the comments is using combineLatest:

combineLatest(books, authors).subscribe(response => {
 ...
})
Sign up to request clarification or add additional context in comments.

5 Comments

He can use combineLatest, too. There is this thread: stackoverflow.com/questions/41797439/…
@ChristianBenseler, nice one. Thanks for that!
you could update your answer with the combineLatest so it will have more information for the OP.
@ChristianBenseler not 100% sure on the syntax since I have never personally used combineLatest but feel free to edit and add it if you would like.
The usage is similar to forkJoin: you pass an array of observables and subscribe to it. Example here: github.com/btroncone/learn-rxjs/blob/master/operators/… note that there is also a combineAll operator.
-1

I don't know if my answer will suit you but, you can create an empty array object in your component and when you do the RESTAPI call you just do a push on the object.

Something like:

private arrayObject: any = [];

this.http.getbooks().subscribe(
    data=>{
    this.getbooksinfo=data;
    this.arrayObject.push(this.getbooksinfo);
    }
)
this.http.getauthors().subscribe(
    data=>{
    this.authors=data;
    this.arrayObject.push(this.authors);
    }

)

EDITED ---

Following edkeveked comment, you can user forkJoin, but need to change a little bit your code.

import { forkJoin } from "rxjs/observable/forkJoin";
    let apibooks = this.httpClient.get('YOU_API_URL');
    let apiauthors = this.httpClient.get('YOU_API_URL');

    forkJoin([apibooks, apiauthors]).subscribe(results => { console.log(results); // will print array of results;
console.log(results[0]); // will print apibooks results;
console.log(results[1]); // will print apiauthors results;
});

Print to console log so you can understand whats happening.

1 Comment

But using this approach, he won't be able to "observe" when both values are emitted. The best way, using rxjs (and following the angular approach) is to use rxjs' operators, such as combineLatest or forkJoin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.