Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何返回两个作为同一个可观察对象连接的可观察对象_Typescript_Rxjs - Fatal编程技术网

Typescript 如何返回两个作为同一个可观察对象连接的可观察对象

Typescript 如何返回两个作为同一个可观察对象连接的可观察对象,typescript,rxjs,Typescript,Rxjs,我很难让函数返回。它有一个条件可以返回一个可观察的,另一个条件是我想返回两个可观察的结果,以及成功合并在一起的结果 像这样的 getSearchFeed(): Observable<items[]> { if (this.condition) { return this.populateItemsArray(); //function Returns Items Array Observable } //second condit

我很难让函数返回。它有一个条件可以返回一个可观察的,另一个条件是我想返回两个可观察的结果,以及成功合并在一起的结果

像这样的

getSearchFeed(): Observable<items[]> {
   if (this.condition) {
     return this.populateItemsArray();            //function Returns Items Array Observable
   } 

   //second condition
   const someItems = this.populateSearch();       //function Returns Items Array Observable
   const otherItems = this.populateOtherSearch(); //function Returns Items Array Observable

   return forkJoin(someItems,otherItems)
    .pipe((res:Array) => {
      return [...res[0],...res[1]];
   });
}

对于这种情况,可以使用
toArray()
RxJS操作符。根据,使用
toArray()
运算符

收集所有源发射,并在源完成时将其作为阵列发射

这就是您的代码应该是什么样子。这样做将把返回的观测值连接到一个数组中

import { forkJoin } from 'rxjs';
import { toArray } from 'rxjs/operators';

getSearchFeed(): Observable<items[]> {
   if (this.condition) {
     return this.populateItemsArray();            //function Returns Items Array Observable
   } 

   //second condition
   const someItems = this.populateSearch();       //function Returns Items Array Observable
   const otherItems = this.populateOtherSearch(); //function Returns Items Array Observable

   return forkJoin(someItems,otherItems)
     .pipe(
       toArray(),
     );
}
从'rxjs'导入{forkJoin};
从“rxjs/operators”导入{toArray};
getSearchFeed():可观察{
如果(本条件){
返回this.populateItemsArray();//函数返回数组中可观察的项
} 
//第二个条件
const someItems=this.populateSearch();//函数返回可观察的项数组
const otherItems=this.populateOtherSearch();//函数返回可观察的项数组
返回叉连接(某些项目、其他项目)
.烟斗(
toArray(),
);
}

编辑:我刚刚注意到返回类型,我应该将返回的观测值展平为一个数组。在这种情况下,只需在map操作符中使用,就可以将其展平为单个数组

import { forkJoin } from 'rxjs';
import { map } from 'rxjs/operators';

getSearchFeed(): Observable<items[]> {
   if (this.condition) {
     return this.populateItemsArray();            //function Returns Items Array Observable
   } 

   //second condition
   const someItems = this.populateSearch();       //function Returns Items Array Observable
   const otherItems = this.populateOtherSearch(); //function Returns Items Array Observable

   return forkJoin(someItems,otherItems)
     .pipe(
       map(res => res.flat(2))
     );
}
从'rxjs'导入{forkJoin};
从“rxjs/operators”导入{map};
getSearchFeed():可观察{
如果(本条件){
返回this.populateItemsArray();//函数返回数组中可观察的项
} 
//第二个条件
const someItems=this.populateSearch();//函数返回可观察的项数组
const otherItems=this.populateOtherSearch();//函数返回可观察的项数组
返回forkJoin(某些项目,其他项目)
.烟斗(
地图(分辨率=>分辨率平面(2))
);
}

我知道你有权否决这个问题,但你能解释一下为什么我有更多的理由来改进我的问题吗?嘿,我的回答解决了你的问题吗?谢谢你的回答。我相信你可能是对的,但这不是正确的语法。我正在研究toArrayI,我觉得它应该可以工作,但它仍然有一个返回类型的观察我认为这是接近,但它仍然关闭。对我来说,这是有道理的,但当我尝试它时,我在尝试这是“可观察”类型时得到的确切信息不可分配给“可观察”类型。@hi!我刚注意到返回类型。那样的话,我的答案可能有点不对劲。让我编辑一下,我试了第二次编辑。它看起来没有变平,因为它当前是一个可观察的数组,而不是一个项目数组。
import { forkJoin } from 'rxjs';
import { toArray } from 'rxjs/operators';

getSearchFeed(): Observable<items[]> {
   if (this.condition) {
     return this.populateItemsArray();            //function Returns Items Array Observable
   } 

   //second condition
   const someItems = this.populateSearch();       //function Returns Items Array Observable
   const otherItems = this.populateOtherSearch(); //function Returns Items Array Observable

   return forkJoin(someItems,otherItems)
     .pipe(
       toArray(),
     );
}
import { forkJoin } from 'rxjs';
import { map } from 'rxjs/operators';

getSearchFeed(): Observable<items[]> {
   if (this.condition) {
     return this.populateItemsArray();            //function Returns Items Array Observable
   } 

   //second condition
   const someItems = this.populateSearch();       //function Returns Items Array Observable
   const otherItems = this.populateOtherSearch(); //function Returns Items Array Observable

   return forkJoin(someItems,otherItems)
     .pipe(
       map(res => res.flat(2))
     );
}