javascript - 如何将每个用户对象合并到数组的可观察对象中?

标签 javascript firebase rxjs

我的问题是,我应该使用哪些映射运算符将每个对象转换为数组?

首先,我从 friend 节点检索用户 key ,如下图所示。

enter image description here

我想使用这些键从用户节点获取我的 friend 列表。但是,结果作为单独的对象返回,如下图所示。

enter image description here

因此,我无法迭代这些数据来显示 View 。

我什至不确定这种方法是否正确。

这是我到目前为止所做的代码。

    getMyFriendList() {
      this.userService.getMyFriendKeys().switchMap(data => {
        console.log('TEST', data);
        return data;
    }).subscribe(friend => {
      this.userService.getFriends(friend.key).subscribe(res => {
        console.log('TEST2', res);
        // res is an objects
      });
    });
  } 

friend .json:

{
  "5NnRVze6TVUeunLV0KXVmLby03J2": {
    "VkwT5IgS7ZYqDZ4uz5Dh76inrAK2": true,
    "dm8o0podI3gou0ob3wirkiyOGOu1": true
  },
  "VkwT5IgS7ZYqDZ4uz5Dh76inrAK2": {
    "5NnRVze6TVUeunLV0KXVmLby03J2": true
  },
  "dm8o0podI3gou0ob3wirkiyOGOu1": {
    "5NnRVze6TVUeunLV0KXVmLby03J2": true
  }
}

用户.json:

{
  "5NnRVze6TVUeunLV0KXVmLby03J2": {
    "currentActiveStatus": "online",
    "displayName": "Jeff Kim",
    "email": "ziznzkak@gmail.com",
    "photoURL": "https://firebasestorage.googleapis.com/v0/b/chattycherry-3636c.appspot.com/o/user-default.png?alt=media&token=f85be639-9a1c-4c79-a28d-361171358a41",
    "statusMessage": "",
    "uid": "5NnRVze6TVUeunLV0KXVmLby03J2",
    "username": "helloworld"
  },
  "VkwT5IgS7ZYqDZ4uz5Dh76inrAK2": {
    "currentActiveStatus": "offline",
    "displayName": "John Doe",
    "email": "hahehaheha@naver.com",
    "photoURL": "https://firebasestorage.googleapis.com/v0/b/chattycherry-3636c.appspot.com/o/profilepics%2FVkwT5IgS7ZYqDZ4uz5Dh76inrAK2?alt=media&token=e9c2a8dc-5a10-42a7-a9c9-b28d53776f12",
    "statusMessage": "programming is awesome!",
    "uid": "VkwT5IgS7ZYqDZ4uz5Dh76inrAK2",
    "username": "hello"
  },
  "dm8o0podI3gou0ob3wirkiyOGOu1": {
    "currentActiveStatus": "offline",
    "displayName": "Sia ",
    "email": "ziznzkak4@daum.net",
    "gender": "Female",
    "photoURL": "https://firebasestorage.googleapis.com/v0/b/chattycherry-3636c.appspot.com/o/profilepics%2Fd65DhlrcCGaayOBaF2siVhauQbq1?alt=media&token=19e2b040-0fd9-4f59-9d56-406a817f58a3",
    "statusMessage": "Hmm?",
    "uid": "dm8o0podI3gou0ob3wirkiyOGOu1",
    "username": "hellowo"
  }
}

最佳答案

您可以使用Object.keys() :

如果您想并行调用 getFriends() 并获取 friend 的所有详细信息,您可以使用 Observable.forkJoin():

this.userService.getMyFriendKeys()
    .switchMap(data => {
        return Observable.forkJoin(Object.keys(data).map(x => this.userService.getFriends(x.key)));
    })
    .subscribe(friends => {
        //friends is an array of items
        console.log(friends[0].displayName)//gives John Doe
    })

技巧是在调用 .getMyFriendKeys() 后使用 .map() 返回 Observables 数组。请注意,这个 .map() 是数组的函数,而不是可观察的函数。

关于javascript - 如何将每个用户对象合并到数组的可观察对象中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47829147/

相关文章:

Angular/RXJS,如何根据特定字符串值对对象的可观察数组进行排序?

javascript - RXJS Observable Subscribe 不会触发成功和错误的任何场景

javascript - 使用jquery从JSP响应中解析Json

firebase - 如何从测试中访问 Firebase Flutter 插件?

javascript - angularFire 3 路数据绑定(bind)不会更新函数

swift - Firebase 2.0 - 从 FIRDataSnapshot 读取数据不起作用

javascript - 获取对父 IFRAME 的引用

javascript - 有什么方法可以为Google Map API3绘图库执行overlayBegin事件吗?

javascript - MC-Cordova-Plugin 不工作 [ionic - Angular]

Angular2 RxJS - 为模拟服务创建可观察对象