检索具有一系列数据的可观察

发布于 2025-01-21 20:39:54 字数 625 浏览 0 评论 0原文

想象一下,我有一系列可观察的物品,在这一可观察到的阵列中,我还有其他参考阵列,我希望能够检索具有参考阵列数组的可观察到的可观察到的东西,我有点粘在它上几天,还有其他方法吗?

export class ReferenceData {
  id: number;
  caption: string;

  constructor(_id: number, _caption: string) {
    this.id = _id
    this.caption = _caption
  }
}

const observables = [
 of([new ReferenceData(1, 'Test'), new ReferenceData(2, 'Test 2')]),
];

const getData = (): Observable<[ReferenceData[]]> {
 return observables.map((obs) => {
  return obs.pipe(
    map((data) => {
      return [...data]
    })
  )
 })
}

从getData函数中预期的返回:可观察的&lt; [referenceedata []]&gt;

Imagine, I have an array of observables, inside this array of observables I have other arrays of ReferenceData, I want to be able to retrieve an Observable with an Array of ReferenceData arrays, I'm a bit stuck on it for a couple of days, is there any other way to do that?

export class ReferenceData {
  id: number;
  caption: string;

  constructor(_id: number, _caption: string) {
    this.id = _id
    this.caption = _caption
  }
}

const observables = [
 of([new ReferenceData(1, 'Test'), new ReferenceData(2, 'Test 2')]),
];

const getData = (): Observable<[ReferenceData[]]> {
 return observables.map((obs) => {
  return obs.pipe(
    map((data) => {
      return [...data]
    })
  )
 })
}

Return expected from getData function: Observable<[ReferenceData[]]>

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

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

发布评论

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

评论(2

还不是爱你 2025-01-28 20:39:54

您可以使用如下: -

forkJoin(observables).subscribe((res: ReferenceData[][]) => {
   // your logic here
})

You can use forkJoin operator like below :-

forkJoin(observables).subscribe((res: ReferenceData[][]) => {
   // your logic here
})
话少情深 2025-01-28 20:39:54
const getData = () => from(obs).pipe(switchMap(ob => ob), toArray()).subscribe(console.log)
const getData = () => from(obs).pipe(switchMap(ob => ob), toArray()).subscribe(console.log)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文