Help us understand the problem. What is going on with this article?

RxJS の Operators (12) - Operators to Convert Observables

More than 3 years have passed since last update.

この記事は bouzuya's RxJS Advent Calendar 2015 の 14 日目です。

はじめに

今日は ReactiveX の Operators to Convert Observables について RxJS の API ドキュメントを読んだりサンプルコードを書いたりしていきます。

また RxJS 4.0.7 を対象にしています。

Operators to Convert Observables

Observable.prototype.toArray

流れるデータを Array にまとめて流します。

import { Observable } from 'rx';

Observable
  .from([1, 2, 3])
  .toArray()
  .subscribe(
    value => console.log(`onNext: ${value}`),
    error => console.log(`onError: ${error}`),
    () => console.log('onCompleted')
  );
// onNext: 1,2,3
// onCompleted

特に難しくないです。これで相互変換できるので、以下のようなこともできます、意味はないですが。

import { Observable } from 'rx';

Observable
  .from([1, 2, 3])                          // -3-2-1->
  .toArray()                                // -[1,2,3]->
  .flatMap(array => Observable.from(array)) // -3-2-1->
  .subscribe(
    value => console.log(`onNext: ${value}`),
    error => console.log(`onError: ${error}`),
    () => console.log('onCompleted')
  );
// onNext: 1
// onNext: 2
// onNext: 3
// onCompleted

Observable.prototype.toMap

ES6 Map に変換します。

import { Observable } from 'rx';

Observable
  .from([1, 2, 3])
  .toMap(value => `key${value}`)
  .map(map => Array.from(map.keys()).map(k => `${k}:${map.get(k)}`))
  .subscribe(
    value => console.log(`onNext: ${value}`),
    error => console.log(`onError: ${error}`),
    () => console.log('onCompleted')
  );
// onNext: key1:1,key2:2,key3:3
// onCompleted

特に言うことはないです。

Observable.prototype.toSet

ES6 Set に変換します。

import { Observable } from 'rx';

Observable
  .from([1, 2, 3, 2])
  .toSet()
  .map(set => Array.from(set.values()))
  .subscribe(
    value => console.log(`onNext: ${value}`),
    error => console.log(`onError: ${error}`),
    () => console.log('onCompleted')
  );
// onNext: 1,2,3
// onCompleted

特に言うことはないです。

おわりに

ReactiveX の Operators to Convert Observables を見ました。

どうということはないですね。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away