Angular 6 - 比较两个可观察的长度 [英] Angular 6 - compare two observables lengths

查看:26
本文介绍了Angular 6 - 比较两个可观察的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法比较两个 observable 的当前长度?

Is there a way to compare the current length of two observables?

它是将数组与可观察对象进行比较.我想将所有元素与它们进行比较(请参见下面的示例).

It is to compare the arrays withing the observables. I want to compare all the elements count withing them(See example below).

在我尝试这样做的时候,它们已经完全加载了.不等待任何数据.

At the point I am trying to do it, they are already completely loaded. Not waiting on any data.

例如我想做:

if (array1.length < array2.length) {
    array1.foreach(item => item.isSelected = true);

我尝试使用管道、地图、点击,但它们在一段时间后解决并且对我不起作用.我知道这可能违反了可观察对象的概念,但想知道是否有办法做到这一点?

I tried using pipe, map, tap, but they resolve after some time and don't work for me. I know this might be a breaking of obserables concepts, but was wondering if there is an option to do it?

推荐答案

使用一个 Observable 的解析值到另一个中.

Use the resolved values of one Observable inside the other.

ob1 = of([1,2,3])
ob2 = of([1,2,3, 4]);


  foo() {
  this.ob1.pipe(
    switchMap((data1)=> {
      return this.ob2.pipe(
        map((data2) => {
          console.log(data1, data2); // will print [1,2,3] [1,2,3,4],
                                     //  which now you can compare easily
        })
      )
    })
  ).subscribe();
}

https://stackblitz.com/edit/angular-jmgdyk?file=src%2Fapp%2Fapp.component.ts

这篇关于Angular 6 - 比较两个可观察的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆