如何过滤来自商店的对象数组通过选择器

问题描述

我想过滤来自状态的数组并返回一个可观察的布尔值。是我的选择器

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))
)