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

Javascript 如何将每个用户对象合并到数组的可观察对象?,javascript,firebase,rxjs,Javascript,Firebase,Rxjs,我的问题是,我应该使用哪些映射运算符将每个对象转换为数组? 首先,我从friends节点检索用户密钥,如下图所示 我想使用这些键从用户节点获取我的好友列表。但是,结果作为单独的对象返回,如下图所示 因此,我无法迭代这些数据来显示视图 我甚至不确定这种方法是否正确 这是我到目前为止所做的代码 getMyFriendList() { this.userService.getMyFriendKeys().switchMap(data => { consol

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

首先,我从friends节点检索用户密钥,如下图所示

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

因此,我无法迭代这些数据来显示视图

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

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

    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
      });
    });
  } 
friends.json:

{
  "5NnRVze6TVUeunLV0KXVmLby03J2": {
    "VkwT5IgS7ZYqDZ4uz5Dh76inrAK2": true,
    "dm8o0podI3gou0ob3wirkiyOGOu1": true
  },
  "VkwT5IgS7ZYqDZ4uz5Dh76inrAK2": {
    "5NnRVze6TVUeunLV0KXVmLby03J2": true
  },
  "dm8o0podI3gou0ob3wirkiyOGOu1": {
    "5NnRVze6TVUeunLV0KXVmLby03J2": true
  }
}
Users.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"
  }
}
您可以使用:

如果要并行调用
getFriends()
,并获取好友的所有详细信息,可以使用
Observable.forkJoin()


诀窍是在调用
.getMyFriendKeys()
后,使用
.map()
返回一个观察值数组。注意这个
.map()
是数组的函数,不是可观测的。

我自己弄明白了。我应该使用combineLatest()方法。 首先,从firebase中提取两个不同的对象可观测值,然后将friends对象可观测值合并为一个新的可观测值,名为
this.friends
。这将产生一个可观察的数组,我可以在模板中迭代该数组

谢谢你的帮助

    getMyFriendList() {
      this.userService.getMyFriendKeys().switchMap(data => {
        return Observable.combineLatest(data.map(x => this.userService.getFriends(x.key)));
    }).subscribe(friends => {
        this.friends = friends;
    });
  }

能否正确提供
getMyFriendKeys()
返回的主体的json格式。其中一个是由
getFriends()
返回的,谢谢您的回答。但是控制台上没有打印出来。你能检查一下服务是否正在获取数据吗?检查控制台中的网络选项卡。另外,如果您可以提供数据结构的json格式也很好。我已经编辑了您的问题,并将我的答案更新为静止,它没有给出任何信息:(@JeffMinsungKim)如果这些服务被触发,您需要检查您的网络选项卡。请注意,Observable.forkjoin()将等待所有http请求完成,然后才会发出发射
    getMyFriendList() {
      this.userService.getMyFriendKeys().switchMap(data => {
        return Observable.combineLatest(data.map(x => this.userService.getFriends(x.key)));
    }).subscribe(friends => {
        this.friends = friends;
    });
  }