我如何返回RXJ中Allproducts $的IEMPHOSERES的IEMPHOSERES?

发布于 2025-02-09 05:43:54 字数 789 浏览 2 评论 0原文

我想从具有Allproducts $的匹配productName的GetEmployeeById函数中返回可观察的可观察。

allProducts$ = this.state$.pipe(
map(x => x.allProducts as IProducts[] || [] as IProducts[]),
distinctUntilChanged()); 

GetEmployeeByID(Id: number): Observable<IEmployees> {
  return this.allEmployees$.pipe(
    map(res => <IEmployees>res.find(data => data.id === Id) ?? { id: -1, name: "N/A", email: "N/A", gender: "N/A", productid: -1, productName: "N/A" } as IEmployees),
    switchMap((Employee: IEmployees) => {
       return this.allProducts$.subscribe(allProducts => {
        return of({
          ...Employee,
          productName: allProducts.find(Product => Product.productid == Employee.productid)?.name
        }) as IEmployees
       })
    })
  )
}

I want to return Observable from GetEmployeeByID function that has a matching productName from allProducts$:

allProducts$ = this.state$.pipe(
map(x => x.allProducts as IProducts[] || [] as IProducts[]),
distinctUntilChanged()); 

GetEmployeeByID(Id: number): Observable<IEmployees> {
  return this.allEmployees$.pipe(
    map(res => <IEmployees>res.find(data => data.id === Id) ?? { id: -1, name: "N/A", email: "N/A", gender: "N/A", productid: -1, productName: "N/A" } as IEmployees),
    switchMap((Employee: IEmployees) => {
       return this.allProducts$.subscribe(allProducts => {
        return of({
          ...Employee,
          productName: allProducts.find(Product => Product.productid == Employee.productid)?.name
        }) as IEmployees
       })
    })
  )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脱离于你 2025-02-16 05:43:54

与其订阅allproducts $ switchmap 内部的,通过它在管道内部执行所需的操作,然后返回其结果可观察到的。有关上下文,请参见下面的代码。

class Foo {
allProducts$ = this.state$.pipe(
map(x => <IProducts[]> (x.allProducts ?? [])),
distinctUntilChanged()
);

public GetEmployeeByID(Id: number): Observable <IEmployees> {
return this.allEmployees$.pipe(
map(res => <IEmployees> res.find(data => data.id === Id) ?? {
id: -1,
name: "N/A",
email: "N/A",
gender: "N/A",
productid: -1,
productName: "N/A"
}),
switchMap((Employee: IEmployees) => {
return this.allProducts$.pipe( //

Instead of subscribing allProducts$ inside switchMap, pipe through it, do what you need inside the pipe, and return its resulting observable. See the code below for context.

class Foo {
  allProducts$ = this.state$.pipe(
    map(x => <IProducts[]> (x.allProducts ?? [])),
    distinctUntilChanged()
  );
  
  public GetEmployeeByID(Id: number): Observable <IEmployees> {
    return this.allEmployees$.pipe(
      map(res => <IEmployees> res.find(data => data.id === Id) ?? {
        id: -1,
        name: "N/A",
        email: "N/A",
        gender: "N/A",
        productid: -1,
        productName: "N/A"
      }),
      switchMap((Employee: IEmployees) => {
        return this.allProducts$.pipe(  // ???? pipe through `allProducts

Instead of subscribing allProducts$ inside switchMap, pipe through it, do what you need inside the pipe, and return its resulting observable. See the code below for context.

map((allProducts) => <IEmployees> ({ ...Employee, productName: allProducts.find(Product => Product.productid == Employee.productid)?.name })) ) }) ) } }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文