从 angularfire2 查询子集 [英] Querying subset from angularfire2
本文介绍了从 angularfire2 查询子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从 firebase 列表中获取一个子集.假设我有两套:
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
}
和
set2 = {
dog:true,
cat:true
}
我需要的是获取在 set1 中,但不在 set2 中",在这种情况下它会返回:
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.
提前致谢.
推荐答案
您可以使用 combineLatest
操作符来组成一个 observable,它发出一个包含 set1
中不在 set1
中的键/值的对象代码>set2:
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屋!
查看全文