如何在角度6中合并两个相同类型的可观察数组?

Top*_*000 0 rxjs typescript angular

我似乎无法弄清楚如何组合两个可观察的数组。这是使用Angular 6,rxjs 6.3.2

示例代码:

var arrayOfUsers = this.httpCallToGetObservableArrayOfUsers();
var anotherArrayOfUsers = this.httpCallToGetADifferentArrayOfUsers();

//how do I combine these two Observable<User[]>?
Run Code Online (Sandbox Code Playgroud)

提前致谢

Que*_*nck 5

由于您的可观察对象是http调用,因此我假设它们在发出后完成。您可以使用forkJoin组合它们:

var arrayOfUsers = this.httpCallToGetObservableArrayOfUsers();
var anotherArrayOfUsers = this.httpCallToGetADifferentArrayOfUsers();

const allUsersArray = forkJoin(arrayOfUsers, anotherArrayOfUsers).pipe(
    map(([users, otherUsers]) => users.concat(otherUsers))
);
Run Code Online (Sandbox Code Playgroud)

如果您的观测值没有完成,请使用CombineLatest:

const allUsersArray = combineLatest(arrayOfUsers, anotherArrayOfUsers).pipe(
    map(([users, otherUsers]) => users.concat(otherUsers))
);
Run Code Online (Sandbox Code Playgroud)