窗口状态

发布于 2025-02-12 21:54:05 字数 128 浏览 1 评论 0原文

我有一个可观察到的数字[1,3,3,5,2,4,1,3],并希望通过两个部门的剩余部分组对其进行窗口。因此,可观察到的结果将是[[1,3,5],[2,4],[1,3]]。我该怎么做?

I have an observable of numbers [1, 3, 5, 2, 4, 1, 3] and want to window it by groups of same remainder of the division by two. So, resulting observable of observables will be [[1, 3, 5], [2, 4], [1, 3]]. How can I do it?

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

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

发布评论

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

评论(1

青衫负雪 2025-02-19 21:54:05

以下片段可以产生您想要的结果,尽管我不确定您想实现什么目标。

降低将在最后一个子阵列中添加数字,直到其中一个与其他结果除以两个相同的结果。

地图将子阵列的数组转换为可观察到的数组

from([1, 3, 5, 2, 4, 1, 3])
  .pipe(
    reduce((result, num) => {
      const lastArray = result[result.length - 1];
      if (lastArray.length > 0 && lastArray[0] % 2 == num % 2) {
        lastArray.push(num);
        result[result.length - 1] = lastArray;
      } else {
        result.push([num]);
      }
      return result;
    }, [[]]),
    map((array: any[]) => array.map((subarray) => from(subarray)))
  )
  .subscribe((res) => {
    res.forEach((obs, i) =>
      obs.subscribe((data) => console.log(data, 'observable ' + i))
    );
  });

The below snippet can produce the results you want, although I'm not sure what you are trying to achieve with this.

The reduce will add numbers to the last subarray, until one of them does not have the same result with the others when divided by two.

The map converts the array of subarrays to array of observables

from([1, 3, 5, 2, 4, 1, 3])
  .pipe(
    reduce((result, num) => {
      const lastArray = result[result.length - 1];
      if (lastArray.length > 0 && lastArray[0] % 2 == num % 2) {
        lastArray.push(num);
        result[result.length - 1] = lastArray;
      } else {
        result.push([num]);
      }
      return result;
    }, [[]]),
    map((array: any[]) => array.map((subarray) => from(subarray)))
  )
  .subscribe((res) => {
    res.forEach((obs, i) =>
      obs.subscribe((data) => console.log(data, 'observable ' + i))
    );
  });

working example

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