如何合并flatMap发出的?

时间:2018-03-26 14:27:17

标签: angular typescript firebase ionic2 rxjs

在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
  }
] 

log 1:

-L8CmrV8BBQCVv_0x_-6
-L8CnDqHz_VWP9dpw-tz
-L8CxQ_bz4EQSPa4STUd
-L8CxSaF1JdHwhn3YyrC
-L8WVlwWj-ghpZsx2PEE
-L8WW5QbxxNMeWonNdSg
-L8XJHI0-ZhPAA9Xy3Sx

log 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}

log 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合并为一个对象数组而不是分离的对象。

我的目标是:

显示循环遍历数组的离子选择并显示地名。

感谢。

1 个答案:

答案 0 :(得分:2)

如果来源完成:

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