从 angularfire2 查询子集

Querying subset from angularfire2

我需要从 firebase 列表中获取一个子集。假设我有两套:

set1 = {
    dog:true,
    cat:true,
    snake:true,
    bull:true
}

set2 = {
    dog:true,
    cat:true
}

我需要的是 "in set1, but not in set2",在这种情况下它会 return:

setresult = {
    snake:true,
    bull:true
}

我试图用地图实现这个:

this.setresult = this.af.database.list('/set2/').map((animals) => {
        return animals.map((animal) => {
            if (auth.uid == _user.$key || af.database.object('set2/' + animal.$key) == null) {}
            else {

                return af.database.object('set1/' + animal.$key);
            }
        })
    })

但我最终得到一个包含空值的列表,我只需要结果集。

提前致谢。

您可以使用 combineLatest 运算符来组合一个可观察对象,该对象发出一个包含 set1 中的 keys/values 且不在 set2 中的对象:

import * as Rx from "rxjs/Rx";

let set = Rx.Observable.combineLatest(

    // Combine the latest values from set1 and set2
    // using AngularFire2 object observables.

    this.af.database.object('/set1/'),
    this.af.database.object('/set2/'),

    // Use the operator's project function to emit an
    // object containing the required values.

    (set1, set2) => {
        let result = {};
        Object.keys(set1).forEach((key) => {
            if (!set2[key]) {
                result[key] = set1[key];
            }
        });
        return result;
    }
);

set.subscribe((set) => { console.log(set); });