Rxjs只映射第一次发射



有没有一个操作符可以让我只映射第一个发射?

类似的东西

import { from } from 'rxjs';
import { mapFirst } from 'rxjs/operators';
const source = from([1, 2, 3, 4, 5]);
const example = source.pipe(mapFirst(val => val + 10));
//output: 11,2, 3, 4, 5

如果你想写一个用户-陆地运营商来做这件事:

import { OperatorFunction } from "rxjs";
import { map } from "rxjs/operators";
function mapFirst<T, R>(selector: (value: T) => R): OperatorFunction<T, T | R> {
return map<T, T | R>((value, index) => (index === 0) ? selector(value) : value);
}

你会像在你的问题中那样使用它。

使用first运算符或take(1)

const source = from([1, 2, 3, 4, 5]);
const example = source.pipe(first());

最新更新