如何将可观察的对象变成可观察的阵列

发布于 2025-01-29 21:11:05 字数 744 浏览 3 评论 0原文

我的角度项目中有一个可观察到的对象,该对象具有这种类型:

  export interface FavoritesResponse {
    wallet: boolean;
    deposit: boolean;
    withdraw: boolean;
    transfer: boolean;
    exchange: boolean;
    ticket: boolean;
    account: boolean;
  }

我想从此对象中提取array仅具有具有值true的属性。

因此,例如,如果我的最爱对象看起来像这样:

  favorites$ = {
    wallet: true;
    deposit: true;
    withdraw: false;
    transfer: false;
    exchange: false;
    ticket: true;
    account: true;
  }

我想让我的启用favorites $看起来像这样:

  enabledFavorites$ = [
    wallet,
    deposit,
    ticket,
    account
  ]

就像在那样,将其变成一个数组,只有具有真实值的键。我该怎么做?我知道该解决方案可能包含RXJS管道,地图,但我不知道我应该做什么。

I have this observable object in my angular project that has this type:

  export interface FavoritesResponse {
    wallet: boolean;
    deposit: boolean;
    withdraw: boolean;
    transfer: boolean;
    exchange: boolean;
    ticket: boolean;
    account: boolean;
  }

I want to extract an array from this object with only the properties that have the value true.

So for example if my favorites object looks like this:

  favorites$ = {
    wallet: true;
    deposit: true;
    withdraw: false;
    transfer: false;
    exchange: false;
    ticket: true;
    account: true;
  }

I want to have my enabledFavorites$ look like this:

  enabledFavorites$ = [
    wallet,
    deposit,
    ticket,
    account
  ]

as in, turn it into an array and only have the keys that had the value of true. How can I do this? I know the solution probably contains an rxjs pipe, map but I don't know what I should be doing exactly.

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

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

发布评论

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

评论(3

年华零落成诗 2025-02-05 21:11:06

如果您说可观察到的可观察到的对象fairiteSresponse,并且希望将排放转换为仅带有值true的键的数组,则可以使用

  1. RXJS < a href =“ https://rxjs.dev/api/operators/map” rel =“ nofollow noreferrer”> map 操作员转换输入对象
  2. JS方法 object.keys.keys.keys() a> with执行实际转换
enabledFavorites$: Observable<string[]> = favorites$.pipe(
  map((favs: FavoritesResponse) => 
    Object.keys(favs).filter((key: string) => !!favs[key])
  )
);

If you mean to say the observable emits an object of type FavoritesResponse and you wish to transform the emission to an array of it's keys only with value true, you could use

  1. RxJS map operator to transform the incoming object
  2. Native JS methods Object.keys() with Array#filter to perform the actual transformation
enabledFavorites$: Observable<string[]> = favorites$.pipe(
  map((favs: FavoritesResponse) => 
    Object.keys(favs).filter((key: string) => !!favs[key])
  )
);
挽清梦 2025-02-05 21:11:06
get favoritesArray$(): Observable<string[]> {
return this.favoritesSettings$.pipe(
  map((favorites) => {
    const _items = Object.keys(favorites);
    const _result: string[] = [];

    _items.forEach((item) => {
      if (!!(favorites as any)[item]) {
        _result.push(item);
      }
    });

    return _result;
  })
);

}

get favoritesArray$(): Observable<string[]> {
return this.favoritesSettings$.pipe(
  map((favorites) => {
    const _items = Object.keys(favorites);
    const _result: string[] = [];

    _items.forEach((item) => {
      if (!!(favorites as any)[item]) {
        _result.push(item);
      }
    });

    return _result;
  })
);

}

最终幸福 2025-02-05 21:11:06

因此,我想您要从可观察的&lt; fairitesRessponse&gt;转换为observable&lt; string []&gt;带有string []包含键检查。

一种方法可能是:

  const enabledFav$ = favOptions$.pipe(
    map(resp => {
      let result = [];
      Object.keys(resp).forEach(key => {
        if (resp.key) {
          result.push(key);
        }
      });
      return result;
    })
  );

我不知道场景,但这基本上是代码。

这里enabledfav $observable&lt; string []&gt; and favoptions $ is observable&lt; fairter&lt; fairitesResponse&gt;

So I guess that you want is convert from Observable<FavoritesResponse> to Observable<string[]> with the string[] containing the keys checked.

One way could be:

  const enabledFav$ = favOptions$.pipe(
    map(resp => {
      let result = [];
      Object.keys(resp).forEach(key => {
        if (resp.key) {
          result.push(key);
        }
      });
      return result;
    })
  );

I dont know the scenario but this is basically the code.

Here enabledFav$is Observable<string[]> and favOptions$ is Observable<FavoritesResponse>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文