合并数组对象的可观察对象

时间:2019-01-13 13:31:38

标签: javascript rxjs

如何使用rxjs合并两个或多个可观察到的数组(即Observable<Object[]>, Observable<Object[]>)以返回一个Observable<Object[]>

forkJoinmerge分别发出两个Observable<Object[]>数组。

getEmployeesLeavesByEmployeeNumber2(employeeNumber,afromDate,atoDate) {
    const scenario1 = this.afs.collection(`${environment.FB_LEAVES}`, ref => {
        let query: firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
        query = query.where("employeeNumber", "==", employeeNumber);
        query = query.where("fromDate",">=",afromDate);
        query = query.where("fromDate","<=",atoDate);
        return query;

    }).snapshotChanges()
        .pipe(take(1))
        .pipe(
            map(changes => {
                return changes.map(a => {
                    const data = a.payload.doc.data() as Leave;
                    data.docId = a.payload.doc.id;
                    return data;
                })
            })
        ).pipe(map(leaves => {
            let leavesArr=leaves.filter(leave => leave.status!==environment.LEAVE_STATUS_DECLINED)
            return leavesArr;
        }));

    const scenario2 = this.afs.collection(`${environment.FB_LEAVES}`, ref => {
        let query: firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
        query = query.where("employeeNumber", "==", employeeNumber);
        query = query.where("toDate","<=",afromDate);
        query = query.where("toDate","<=",atoDate);
        return query;

    }).snapshotChanges()
        .pipe(take(1))
        .pipe(
            map(changes => {
                return changes.map(a => {
                    const data = a.payload.doc.data() as Leave;
                    data.docId = a.payload.doc.id;
                    return data;
                })
            })
        ).pipe(map(leaves => {
            let leavesArr=leaves.filter(leave => leave.status!==environment.LEAVE_STATUS_DECLINED)
            return leavesArr;
        }));

    const scenario3 = this.afs.collection(`${environment.FB_LEAVES}`, ref => {
        let query: firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
        query = query.where("employeeNumber", "==", employeeNumber);
        query = query.where("fromDate","<=",afromDate);
        return query;

    }).snapshotChanges()
        .pipe(take(1))
        .pipe(
            map(changes => {
                return changes.map(a => {
                    const data = a.payload.doc.data() as Leave;
                    data.docId = a.payload.doc.id;
                    return data;
                })
            })
        ).pipe(
            filter(leave => {
                return leave!==undefined;
            })
        );
    return merge(scenario1,scenario2);
}

我期望一个可观察的数组,但得到2即。

emp's leaves:  [{…}]
assign.component.ts:198 leaves array length at assignment error  1
assign.component.ts:168 emp's leaves:  (2) [{…}, {…}]
assign.component.ts:198 leaves array length at assignment error  2

1 个答案:

答案 0 :(得分:0)

我可以使用它来工作:

return forkJoin(scenario1,scenario2).pipe(map((arr) => [...arr[0],...arr[1]] ));