Angular9 / rxjs6.5:如何将可观察对象数组转换为可观察对象属性数组?

时间:2020-06-26 15:19:48

标签: rxjs observable angular9 rxjs6 switchmap

我正在使用Angular(9)Powered Bootstrap(ng-bootstrap 6.1),并为ngbTypeahead提供了Observable ,如下所示:

search = (text$: Observable<string>) => {
    return text$.pipe(
      debounceTime(200),
      distinctUntilChanged(),
      // switchMap allows returning an observable rather than maps array
      switchMap((searchText) => {
        if (!searchText || searchText.trim().length < 3) {
          // when the user erases the searchText
          return EMPTY;
        } else {
          // get a list of dealer reps
          return this.myService.getKeyValues(searchText);
        }
      })
      , catchError((error) => of(this.myService.postErrorMessage(error)))
    );
  }

我需要通过将每个IKeyValue项目映射到它的item.value属性来将this.myService.getKeyValues的输出从Observable 转换为Observable

我如何使用rxjs 6.5.5修改上面的代码来做到这一点?

1 个答案:

答案 0 :(得分:1)

在switchMap之后,您可以放置​​一个moving metadata to the front运算符,进行所需的转换并返回新值。

我认为这将符合这些原则。请注意,返回值中的map()是不同的。

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"username":"xyz", "preference":"123"}' \
  http://localhost:3000/mih/search

}