how to build single object from 2 http requests in angular, without adding another value

前端 未结 2 1548
谎友^
谎友^ 2020-12-22 10:19

If you look at this question. the problem was really simple, but solutions were more complicated, to address more details I am asking this question.

If you look at

2条回答
  •  没有蜡笔的小新
    2020-12-22 11:08

    I think you can use RxJs combineLast to achieve what you want.

    If you combine the result of the first call and the next call with combineLast will be able to subscribe and get both objects and return a combination of both of them.

    Here's an example:

    const objA = {xpto: 'xis'};
    const objB = {moreData: 'pato'};
    
    
    of(objA).pipe(
      map((a) => {
        return combineLatest(of(a), of(objB));
      })
    ).subscribe((result) => {
      result.pipe(
        map(([a, b]) => {
          return {
            newXpto: a.xpto,
            newMoreData: b.moreData
          };
        })
      ).subscribe((combinedObjects) => {
        console.log(combinedObjects);
       })
    })
    

    Something like this should work. I don't know if it is the more straigthfoward answer but I did it quickly. Nevertheless it gives you a sense of how you could do it.

提交回复
热议问题