如何解决如何过滤来自商店的对象数组通过选择器
我想过滤来自状态的数组并返回一个可观察的布尔值。是我的选择器
export const customArray = createSelector(
arrayCustomizations,(entities: Dictionary<Customizations>,props: { id: string }) => {
const customizations = entities[props.id];
return customizations.allCustoms;
},);
这将返回一个对象数组。我想获取所有具有'name'属性的对象,例如object.name!== null并将其分配给此对象
buttonDisabled$=
如果我订阅了此选择器,则可以过滤并获取所需的内容,但这不是一个可观察的值。
解决方法
您可以为此使用pipe
的{{1}}方法。这将为您带来可观察的价值。
像这样使用。
rxjs
,
如果在选择器中操作数据,这将是高效且高效的。 createSelector
被记住并跟踪值的变化,并且如果值不变,则不会将值传播给侦听器。
export const customArray = createSelector(
arrayCustomizations,(entities: Dictionary<Customizations>,props: { id: string }) => {
const customizations = entities[props.id];
return customizations.allCustoms.filer(o => o.name != null);
},);
基于以下替代方法的评论可能是
在selector.ts中
export const customArray = createSelector(
arrayCustomizations,props: { id: string }) => {
const customizations = entities[props.id];
return customizations.allCustoms;
},);
在component.ts
中this.result$ = this.store.pipe(select(customArray,{ id: '123' })).pipe(
map(result => result.filter(a => a.name != null))
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。