从angularfire2查询子集 [英] Querying subset from angularfire2

查看:174
本文介绍了从angularfire2查询子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Firebase列表中获取子集。

pre $ set
$ b $ true $ $ b $ cat $ true $ $ $ $ $ $ $ $ $
snake:true,
bull:true
}



  set2 = {
dog:true,
cat:true
}

我需要的是在set1中,而不是在set2中,在这种情况下,它会返回:

  setresult = {
snake:true,
bull:true
}

我试图用map实现:

<$ p $ (动物)=> {
return animals.map((animal)=> ; {
if(auth.uid == _user。$ key || af.database.object('set2 /'+ animal。$ key)== null){}
else {

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

但是,我最终无线第一个用NULL的列表,我只需要结果集。



在此之前感谢。

解决方案

您可以使用 combineLatest 操作符组成一个observable,它发出一个包含 set1 中的键/值的对象不在 set2

 导入*作为Rx从rxjs / Rx ; 

let set = Rx.Observable.combineLatest(

//使用AngularFire2对象的observables组合set1和set2
的最新值

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

//使用运算符的项目函数发出一个
//包含所需值的对象
$ b $(set1,set2)=> {
let result = {};
Object .key(set1).forEach((key)=> {
if(!set2 [key]){
result [key] = set1 [key];
}
});
返回结果;
}
);

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


I need to get a subset from a firebase list. Let's say I have two sets:

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

and

set2 = {
    dog:true,
    cat:true
}

What I need is to get the "in set1, but not in set2", in this case it would return:

setresult = {
    snake:true,
    bull:true
}

I tried to achieve this with map:

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);
            }
        })
    })

But I end up with a list with nulls, and I need only the result set.

Thanks in advance.

解决方案

You can use the combineLatest operator to compose an observable that emits an object containing the keys/values from set1 that are not in 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); });

这篇关于从angularfire2查询子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆