switchmap运算符后,我使用flatmap运算符获取对象可观察的数据
return this.db.list(`UserPlaces/${this.authData.auth.auth.currentUser.uid}`, {
query: {
orderByChild: 'deleted',
equalTo: false
}
})
.map((locations: any) => {
console.log(JSON.stringify(locations,null,2)); // this log is called 0
return locations.map(location => {
return location.$key;
});
}).switchMap(ids => {
return ids.map(id => {
console.log(id) // this log is called 1
return this.db.object(`Devices/${id}`)
});
}).flatMap((x: any) => {
console.log(x); // this log is called 2
return x;
}).map((x:any)=>{
console.log(x);
if(!x.deleted){
return x;
}
})
.do(console.log) // and this log is called 3
日志0:
[
{
"deleted": false,
"guest": false,
"owner": true
},
{
"deleted": false,
"guest": false,
"owner": true
},
{
"deleted": false,
"guest": false,
"owner": true
},
{
"deleted": false,
"guest": false,
"owner": true
},
{
"deleted": false,
"guest": false,
"owner": true
},
{
"deleted": false,
"guest": false,
"owner": true
},
{
"deleted": false,
"guest": false,
"owner": true
}
]
日志1:
-L8CmrV8BBQCVv_0x_-6
-L8CnDqHz_VWP9dpw-tz
-L8CxQ_bz4EQSPa4STUd
-L8CxSaF1JdHwhn3YyrC
-L8WVlwWj-ghpZsx2PEE
-L8WW5QbxxNMeWonNdSg
-L8XJHI0-ZhPAA9Xy3Sx
日志2:
FirebaseObjectObservable {_isScalar: false, $ref: U, source: FirebaseObjectObservable, operator: ObserveOnOperator}
FirebaseObjectObservable {_isScalar: false, $ref: U, source: FirebaseObjectObservable, operator: ObserveOnOperator}
FirebaseObjectObservable {_isScalar: false, $ref: U, source: FirebaseObjectObservable, operator: ObserveOnOperator}
FirebaseObjectObservable {_isScalar: false, $ref: U, source: FirebaseObjectObservable, operator: ObserveOnOperator}
FirebaseObjectObservable {_isScalar: false, $ref: U, source: FirebaseObjectObservable, operator: ObserveOnOperator}
FirebaseObjectObservable {_isScalar: false, $ref: U, source: FirebaseObjectObservable, operator: ObserveOnOperator}
FirebaseObjectObservable {_isScalar: false, $ref: U, source: FirebaseObjectObservable, operator: ObserveOnOperator}
日志3:
{deleted: false, id: "", name: "new place", owner: "cS81XkHhVHNv0nmpVit0Glpuc1t1", tone: "default", …}
{deleted: false, id: "", name: "new one", owner: "cS81XkHhVHNv0nmpVit0Glpuc1t1", tone: "default", …}
{deleted: false, id: "", name: "nice", owner: "cS81XkHhVHNv0nmpVit0Glpuc1t1", tone: "default", …}
{deleted: false, id: "", name: "new one", owner: "cS81XkHhVHNv0nmpVit0Glpuc1t1", rooms: {…}, …}
{deleted: false, id: "", name: "das", owner: "cS81XkHhVHNv0nmpVit0Glpuc1t1", tone: "default", …}
我需要的是将日志3合并为对象的数组而不是分开的对象。
我的目标是:
显示一个通过数组循环并显示位置的离子选择。
谢谢。
如果来源完成:
return this.db.list(`UserPlaces/${this.authData.auth.auth.currentUser.uid}`, { ... })
...
.toArray()
.do(console.log) // and this log is called 3
或者如果没有使用scan()
收集结果:
return this.db.list(`UserPlaces/${this.authData.auth.auth.currentUser.uid}`, { ... })
...
.scan((acc, item) => [...acc, item], [])
.do(console.log) // and this log is called 3
顺便说一句,看来您有意地将数组解开:
...
.flatMap((x: any) => {
console.log(x); // this log is called 2
return x;
}
...