首页 > Web开发 > 详细

[RxJS] Filtering operator: first

时间:2020-11-26 18:30:37      阅读:24      评论:0      收藏:0      [点我收藏+]

first(predFn, defVal)

 

first can do the work for both "filter" + "take(1)" which filtering the data and end observable.

// RxJS v6+
import { from } from rxjs;
import { first } from rxjs/operators;

const source = from([1, 2, 3, 4, 5]);
//emit first item to pass test
const example = source.pipe(first(num => num === 5));
//output: "First to pass test: 5"
const subscribe = example.subscribe(val =>
  console.log(`First to pass test: ${val}`)
);

 

// RxJS v6+
import { from } from rxjs;
import { first } from rxjs/operators;

const source = from([1, 2, 3, 4, 5]);
//no value will pass, emit default
const example = source.pipe(first(val => val > 5, Nothing));
//output: ‘Nothing‘
const subscribe = example.subscribe(val => console.log(val));

 

[RxJS] Filtering operator: first

原文:https://www.cnblogs.com/Answer1215/p/14043284.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!