Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rxjs仅映射第一次发射_Rxjs - Fatal编程技术网

Rxjs仅映射第一次发射

Rxjs仅映射第一次发射,rxjs,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 首先使用运算符或取(1) 如果您要编写用户land oper

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

差不多

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

首先使用
运算符或
取(1)


如果您要编写用户land operator来执行此操作:

从“rxjs”导入{OperatorFunction};
从“rxjs/operators”导入{map};
函数mapFirst(选择器:(值:T)=>R):运算符函数{
返回映射((值,索引)=>(索引==0)?选择器(值):值;
}

你可以像在你的问题中一样使用它。

检查索引
映射((值,索引)=>{if(index==0)…}
@cartant谢谢,但我知道我可以这样做,为了更好的语法,我希望有一些自制的操作符:)这不会在第一个值之后发出AnoyMortHanks,但有点遗憾的是,结果不能像
[t,…R]
const source = from([1, 2, 3, 4, 5]);
const example = source.pipe(first());