angular - 如何在 Angular 的 Observable 中使用 Arrays.some?
问题描述
我有调用服务器并检索 id 列表的服务。我想使用某种方法来查找用户的当前 ID 是否等于其中一个 ID。
我试过了:
private isCurrentUserExistsInUserIdsList(): boolean {
return this.userService.findIds().pipe(
map((userIds: number[]) => {
userIds.some(id => {
return id == this.currentUserId;
});
})
);
}
我被困在这里,我怎样才能得到某种方法的结果?
解决方案
我喜欢在同一个服务中关联所有的 observables。这样你就可以
//in your userService:
public isCurrentUserExistsInUserIdsList(currentId:number): Observable<boolean>
{
return this.findIds().pipe(
map((res:any[])=>res.some(x=>x==currentId)))
}
//or, if you use the in the "map" "{" "}" you need use return
public isCurrentUserExistsInUserIdsList(currentId:number): Observable<boolean>
{
return this.findIds().pipe(
map((res:any[])=>{
return res.some(x=>x==currentId)
}))
}
所以,你可以订阅你的组件
this.isCurrentExistInUserIdsList(this.currentUserId).subscribe(res=>{
console.log(res)
})
推荐阅读
- python - 如何在终端中正确打印我的方框?
- swift-package-manager - Swift 包构建正常,但由于派生数据中缺少“SourcePackages”,测试总是失败
- python - python web服务器和客户端服务器
- scikit-learn - clf.pred_proba 用于面向直方图的梯度预测
- database - 如何在VB中显示访问数据库?
- c# - C# 计算小数位数而不考虑尾随零
- azure - AZURE 上的 RBAC 用于运行时
- python - 单独的标签 - Django
- python - 如何使用关联表进行 Django 查询?
- bash - 如何在 bash shell 输出中等待文本?