如何将可观察的对象变成可观察的阵列
我的角度项目中有一个可观察到的对象,该对象具有这种类型:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您说可观察到的可观察到的对象
fairiteSresponse
,并且希望将排放转换为仅带有值true
的键的数组,则可以使用map
操作员转换输入对象object.keys.keys.keys() a> with
执行实际转换
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 valuetrue
, you could usemap
operator to transform the incoming objectObject.keys()
withArray#filter
to perform the actual transformation}
}
因此,我想您要从
可观察的< fairitesRessponse>
转换为observable< string []>
带有string []
包含键检查。一种方法可能是:
我不知道场景,但这基本上是代码。
这里
enabledfav $
是observable< string []>
and favoptions $ isobservable< fairter< fairitesResponse>
So I guess that you want is convert from
Observable<FavoritesResponse>
toObservable<string[]>
with thestring[]
containing the keys checked.One way could be:
I dont know the scenario but this is basically the code.
Here
enabledFav$
isObservable<string[]>
and favOptions$ isObservable<FavoritesResponse>