Angular2:Observables Array Operations

原文链接:https://angular-2-training-book.rangle.io/handout/observables/observables_array_operations.html


Observables Array Operations

In addition to simply iterating over an asynchronous collection, we can perform other operations such as filter or map and many more as defined in the RxJS API. This is what bridges an Observable with the iterable pattern, and lets us conceptualize them as collections.

+

Let’s expand our example and do something a little more with our stream:

+

export class MyApp {
  private doctors = [];

  constructor(http: Http) {
    http.get('http://jsonplaceholder.typicode.com/users/')
        .flatMap((response) => response.json())
        .filter((person) => person.id > 5)
        .map((person) => "Dr. " + person.name)
        .subscribe((data) => {
          this.doctors.push(data);
        });
  }
}

View Example

+

Here are two really useful array operations - map and filter. What exactly do these do?

+

  • map will create a new array with the results of calling a provided function on every element in this array. In this example we used it to create a new result set by iterating through each item and appending the “Dr.” abbreviation in front of every user’s name. Now every object in our array has “Dr.” prepended to the value of its name property.

    +

  • filter will create a new array with all elements that pass the test implemented by a provided function. Here we have used it to create a new result set by excluding any user whose id property is less than six.

    +

Now when our subscribe callback gets invoked, the data it receives will be a list of JSON objects whose id properties are greater than or equal to six and whose name properties have been prepended with Dr..

+

Note the chaining function style, and the optional static typing that comes with TypeScript, that we used in this example. Most importantly functions like filter return an Observable, as in Observables beget other Observables, similarly to promises. In order to use map and filter in a chaining sequence we have flattened the results of our Observable using flatMap. Since filter accepts an Observable, and not an array, we have to convert our array of JSON objects from data.json() to an Observable stream. This is done with flatMap.

+

There are many other array operations you can employ in your Observables; look for them in the RxJS API.

+

rxmarbles.com is a helpful resource to understand how the operations work.

+

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值