重构胖箭头嵌套rxjs流 [英] refactor fat arrow nested rxjs stream

查看:127
本文介绍了重构胖箭头嵌套rxjs流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩 http://reactivex.io/learnrx/ 。更好地学习rxjs。

I am playing with http://reactivex.io/learnrx/. In better effort to learn rxjs.

我正在工作的数组如下。我只是想让这个id从数组中取出。

The array that I am working through is below. I am just trying to get the id's out of the array.

我可以得到答案工作,但觉得答案可以写得更好。

I am able to get the answer to work but feel that answer could be better written.

var movieLists = [
        {
            name: "New Releases",
            videos: [
                {
                    "id": 70111470,
                    "title": "Die Hard",
                    "boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg",
                    "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
                    "rating": 4.0,
                    "bookmark": []
                },
                {
                    "id": 654356453,
                    "title": "Bad Boys",
                    "boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg",
                    "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
                    "rating": 5.0,
                    "bookmark": [{ id: 432534, time: 65876586 }]
                }
            ]
        },
        {
            name: "Dramas",
            videos: [
                {
                    "id": 65432445,
                    "title": "The Chamber",
                    "boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg",
                    "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
                    "rating": 4.0,
                    "bookmark": []
                },
                {
                    "id": 675465,
                    "title": "Fracture",
                    "boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg",
                    "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
                    "rating": 5.0,
                    "bookmark": [{ id: 432534, time: 65876586 }]
                }
            ]
        }
    ];

这是我想出的答案

          movieLists
                  .map( movieList => movieList.videos
                  .map(video => video.id)  )   // don't like this part
                  .concatAll()

我基本上是在另一个地图上嵌套地图,然后调用连接全部

I am basically nesting a map inside of another map then calling concat all.

是否可以重构第二张地图fat arrow,以便它可以坐在第一个之外?

Is it possible to refactor that second map fat arrow so it can sit outside of the first?

.map()
.map()


推荐答案

只需更改运算符的顺序即可。

Just change the order of operators.

Observable.from(movieLists)
  .map(movie => movie.videos)
  .concatAll()
  .map(movie => movie.id);

这篇关于重构胖箭头嵌套rxjs流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆