简体   繁体   English

从.flatMap返回的数组循环/映射

[英]Loop / map over array returned from .flatMap

This is a really entry-level reactive programming question. 这是一个真正的入门级反应式编程问题。 The following code will log an array of users from github. 以下代码将记录来自github的users数组。 How can I get access to loging each individual user.login using Rx? 如何使用Rx访问记录每个user.login

import axios from 'axios'
import Rx from 'rx'

let requestStream = Rx.Observable.just('https://api.github.com/users')

let getJSON = (url) => {
  return axios.get(url).then(response => response.data)
}

let responseStream = requestStream
  .flatMap(function(requestUrl) {
    return Rx.Observable.fromPromise(getJSON(requestUrl))
  })

responseStream.subscribe(function(response) {
  console.log(response)
})

I've tried: 我试过了:

let responseStream = requestStream
  .flatMap(function(requestUrl) {
    return Rx.Observable.fromPromise(getJSON(requestUrl))
  })
  .flatMap(user => {
    console.log(user)
  })

and: 和:

let users = responseStream
  .flatMap(user => {
    console.log(user)
  })

users.subscribe(function(response) {
  // console.log(response)
})

To get each individual user from the array, create and return a new observable of that array as follows: 要从数组中获取每个用户,请创建并返回该数组的新observable,如下所示:

let responseStream = requestStream
  .flatMap(function(requestUrl) {
    return getJSON(requestUrl);
  })
  .flatMap(function(usersResponse) {
    return rx.Observable.from(usersResponse);
  })
  .doOnNext(function(user) {
    console.log(user);
  });

When you call responseStream.subscribe(......) it should now log each user out individually from the .doOnNext() method. 当你调用responseStream.subscribe(......)它现在应该从.doOnNext()方法单独记录每个用户。

I used Mosho's answer as a base for this response with the assumption that the first flatMap returns an array of users from github. 我使用Mosho的答案作为此响应的基础,假设第一个flatMap从github返回一个用户数组。

flatMap handler can return promises, so you don't need fromPromise . flatMap处理程序可以返回promises,因此您不需要fromPromise

To get what you need, an observable of the separate items, you can just use flatMap again, it does exactly what you want. 为了得到你需要的,一个可观察的单独项目,你可以再次使用flatMap ,它完全符合你的要求。 You almost had it right the first try, but the projection (which is merged to a new, returned observable) is what's returned from the flatMap handler. 你在第一次尝试时差不多正确,但是投影(它合并到一个新的,返回的observable)是从flatMap处理程序返回的。

import axios from 'axios'
import Rx from 'rx'

let requestStream = Rx.Observable.just('https://api.github.com/users')

let getJSON = (url) => {
  return axios.get(url).then(response => response.data)
}

let responseStream = requestStream
  .flatMap(function(requestUrl) {
    return getJSON(requestUrl);
  })
  .flatMap(function(x) {
      return x;
  });

responseStream.subscribe(function(response) {
  console.log(response)
})

This way, each element in the array is projected onto a new observable, and they are all merged into a single observable, which is exactly what you want. 这样,数组中的每个元素都被投影到一个新的observable上,它们都被合并到一个observable中,这正是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM