首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rxjs5平坦化的高级实例

rxjs5平坦化的高级实例
EN

Stack Overflow用户
提问于 2017-01-16 09:20:37
回答 1查看 56关注 0票数 0

我的数据结构与此类似:

代码语言:javascript
复制
let userInfo =  [
      {
       id: 'id1',
       users: [
       {
        name: 'userName1',
        job: 'userJob',
       },
       {
        name: 'userName2',
        job: 'userJob',
       }
      ]
     },
      {
       id: 'id2',
       users: [
       {
        name: 'userName3',
        job: 'userJob',
       },
       {
         name: 'userName4',
        job: 'userJob',
       }
      ]
     }
    ]

我期望用户使用RxJS5的新的扁平用户流:

代码语言:javascript
复制
    {
     parent: id,        // parent id, where users[] come from...
     name: 'userName'
     job: 'userJob'
    }

什么是干净的功能方式来存档这个?谢谢..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-16 11:47:54

最简单的方法就是使用concatMap()将用Rx.Observable.from创建的内部可观察到的内容与用父id更新的用户列表合并。

代码语言:javascript
复制
let userInfo =  [
  {
    id: 'id1',
    users: [
      {
        name: 'userName1',
        job: 'userJob',
      },
      {
        name: 'userName2',
        job: 'userJob',
      }
    ]
  },
  {...}
];

let source = Rx.Observable.from(userInfo)
  .concatMap(group => {
    return Rx.Observable.from(group['users'])
      .map(user => {
        user['parent'] = group.id;
        return user;
      });
  });

source.subscribe(
  res => console.log(res)
);

参见现场演示:https://jsbin.com/miximas/2/edit?js,console

已经有很多类似的问题了:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41672956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档