【问题标题】:How to apply filter to pairwise?如何对成对应用过滤器?
【发布时间】:2021-09-22 14:41:41
【问题描述】:

我有直播:

 this.unselectedObjectsIds$ = this.selectedObjectsIds$.pipe(pairwise());

this.selectedObjectsIds$ 在哪里

[
   ['131086', '131089', '131090', '131638', '132139'], 
   ['131086', '131089']
]

我尝试将此过滤器应用于流:

this.unselectedObjectsIds$ = this.selectedObjectsIds$.pipe(
    pairwise(), 
    map((a) => a[0].filter(x => !a[1].includes(x))
);

过滤器是:

a[0].filter(x => !a[1].includes(x));

但这对我不起作用。

【问题讨论】:

  • 为什么需要成对的?

标签: javascript rxjs


【解决方案1】:

pairwise 正在等待下一个值

因为你只有一个值 - 它是数组:[['131086', '131089', '131090', '131638', '132139'], ['131086', '131089']]

如果selectedObjectsIds$BehaviorSubject 例如

您需要发送一个新值 selectedObjectsIds$.next([]) 和您的代码将配对为

[[['131086', '131089', '131090', '131638', '132139'], ['131086', '131089']], []]

我会尝试猜测你将在 OUTPUT 中得到什么:

["131090", "131638", "132139"]吗?

如果是 - 然后使用下一个 sn-p

import { map, pairwise, take, tap } from 'rxjs/operators';
import { BehaviorSubject, interval, of } from 'rxjs';

const selectedObjectsIds$ = new BehaviorSubject([
  ['131086', '131089', '131090', '131638', '132139'],
  ['131086', '131089'],
]);

const unselectedObjectsIds$ = selectedObjectsIds$.pipe(
  tap((data) => {
    console.log(data);
  }),
  map(([arr1, arr2]) => {
    console.log(arr1);
    console.log(arr2);
    return arr1.filter((x) => !arr2.find((i) => i === x));
  })
);
unselectedObjectsIds$.subscribe((data) => console.log(data));


演示:https://stackblitz.com/edit/typescript-i4nazm?file=index.ts

【讨论】:

    猜你喜欢
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 2018-10-21
    • 2019-08-14
    • 1970-01-01
    相关资源
    最近更新 更多