Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Angular 获取与Observable中的条件匹配的第一项_Angular_Typescript_Rxjs_Angular9 - Fatal编程技术网

Angular 获取与Observable中的条件匹配的第一项

Angular 获取与Observable中的条件匹配的第一项,angular,typescript,rxjs,angular9,Angular,Typescript,Rxjs,Angular9,我需要从可观察到的数据中获取有效载荷中的第一项。结果哪种类型是化身: let result = this.fileService.getAvatarByUserId(2).pipe( map((payload: Payload<FileModel[]>) => payload.result), first((result: FileModel[]) => result.type == 'avatar') ); let result=this.fileServic

我需要从可观察到的数据中获取
有效载荷中的第一项。结果
哪种类型是
化身

let result = this.fileService.getAvatarByUserId(2).pipe(
  map((payload: Payload<FileModel[]>) => payload.result),
  first((result: FileModel[]) => result.type == 'avatar')
);
let result=this.fileService.getAvatarByUserId(2.pipe)(
map((有效载荷:有效载荷)=>payload.result),
第一个((结果:FileModel[])=>result.type=='avatar')
);
但我得到的错误是“不可分配给'Observable'类型”

如何解决此问题?

您可以使用and运算符

let result = this.fileService.getFilesByUserId().pipe(
  filter(payload => payload.payload.result === 'avatar'),
  take(1)
);
您可以使用and运算符

let result = this.fileService.getFilesByUserId().pipe(
  filter(payload => payload.payload.result === 'avatar'),
  take(1)
);

您可以根据需要执行以下操作之一

过滤+提取(1)

this.fileService.getAvatarByUserId(2)
.烟斗(
map((有效载荷:有效载荷)=>payload.result),
flatMap(uploads=>uploads),//展平数组
过滤器(upload=>upload.type==='avatar'),//过滤任何属于avatar的内容
take(1)//只返回第一个
).订阅(
console.log
);

第一个(谓词函数)->我的偏好

this.fileService.getAvatarByUserId(2)
      .pipe(
        map((payload: Payload<FileModel[]>) => payload.result),
        flatMap(uploads => uploads), // Flattens the array
        first(upload => upload.type === 'avatar') // returns the first positive match
    ).subscribe(
      console.log,
      console.log // this is to log an error if no match found when the subscription completes
    );
this.fileService.getAvatarByUserId(2)
.烟斗(
map((有效载荷:有效载荷)=>payload.result),
flatMap(uploads=>uploads),//展平数组
第一个(upload=>upload.type=='avatar')//返回第一个正匹配
).订阅(
console.log,
console.log//如果订阅完成时未找到匹配项,则记录错误
);
或 过滤+优先

this.fileService.getAvatarByUserId(2)
      .pipe(
        map((payload: Payload<FileModel[]>) => payload.result),
        flatMap(uploads => uploads), // Flattens the array
        filter(upload => upload.type === 'avatar'), // filters anything that's avatart
        first() // returns the first value
    ).subscribe(
      console.log,
      console.log // this is to log an error if no match found when the subscription completes
    );
this.fileService.getAvatarByUserId(2)
.烟斗(
map((有效载荷:有效载荷)=>payload.result),
flatMap(uploads=>uploads),//展平数组
过滤器(upload=>upload.type==='avatar'),//过滤任何属于avatar的内容
first()//返回第一个值
).订阅(
console.log,
console.log//如果订阅完成时未找到匹配项,则记录错误
);
第一个操作员可以通过向订阅添加错误方法->查看订阅了解最后两个选项(可能是我最喜欢这两个选项的原因…),通知订阅完成时是否未满足条件

看看这个


根据@Jonathan Stellwag的评论(谢谢!!!)对第一个操作员进行了编辑,以增加其清晰度。

您可以根据需要执行以下操作之一

过滤+提取(1)

this.fileService.getAvatarByUserId(2)
.烟斗(
map((有效载荷:有效载荷)=>payload.result),
flatMap(uploads=>uploads),//展平数组
过滤器(upload=>upload.type==='avatar'),//过滤任何属于avatar的内容
take(1)//只返回第一个
).订阅(
console.log
);

第一个(谓词函数)->我的偏好

this.fileService.getAvatarByUserId(2)
      .pipe(
        map((payload: Payload<FileModel[]>) => payload.result),
        flatMap(uploads => uploads), // Flattens the array
        first(upload => upload.type === 'avatar') // returns the first positive match
    ).subscribe(
      console.log,
      console.log // this is to log an error if no match found when the subscription completes
    );
this.fileService.getAvatarByUserId(2)
.烟斗(
map((有效载荷:有效载荷)=>payload.result),
flatMap(uploads=>uploads),//展平数组
第一个(upload=>upload.type=='avatar')//返回第一个正匹配
).订阅(
console.log,
console.log//如果订阅完成时未找到匹配项,则记录错误
);
或 过滤+优先

this.fileService.getAvatarByUserId(2)
      .pipe(
        map((payload: Payload<FileModel[]>) => payload.result),
        flatMap(uploads => uploads), // Flattens the array
        filter(upload => upload.type === 'avatar'), // filters anything that's avatart
        first() // returns the first value
    ).subscribe(
      console.log,
      console.log // this is to log an error if no match found when the subscription completes
    );
this.fileService.getAvatarByUserId(2)
.烟斗(
map((有效载荷:有效载荷)=>payload.result),
flatMap(uploads=>uploads),//展平数组
过滤器(upload=>upload.type==='avatar'),//过滤任何属于avatar的内容
first()//返回第一个值
).订阅(
console.log,
console.log//如果订阅完成时未找到匹配项,则记录错误
);
第一个操作员可以通过向订阅添加错误方法->查看订阅了解最后两个选项(可能是我最喜欢这两个选项的原因…),通知订阅完成时是否未满足条件

看看这个


根据@Jonathan Stellwag的评论(谢谢!!!)对第一个操作符进行了编辑,以增加其清晰度。

我需要将条件应用于每个结果,并获得满足条件的第一个操作符,而不是有效载荷。我只是更新了我的问题来澄清它…@MiguelMoura我更新了答案。如果仍然不是您的要求,请添加更多信息。szenario理解您的问题的最佳案例是提供准确的源(可观测发射+类型)以及您预期的结果(何时+类型)。我需要将条件应用于每个结果,并获得满足条件的第一个结果,而不是有效载荷。我只是更新了我的问题来澄清它…@MiguelMoura我更新了答案。如果仍然不是您的要求,请添加更多信息。szenario理解您的问题的最佳案例是提供准确的源(可观测发射+类型)以及您预期的结果(当+类型)。Take和first不一样。如果您希望在条件没有发生的情况下出现错误,那么首先要做的是。拿第一个和第一个不一样。如果您希望在条件没有发生的情况下出现错误,那么首先要做的是。