可观察的switchMap过滤器toArray

时间:2018-03-28 13:48:37

标签: typescript filter toarray

我尝试使用switchMap& filter

this.getEpisodesForStoryEvents()
    .switchMap(episodes => Observable.from(episodes))
    .filter((episode: Episode) => {
        return episode.ref.startsWith('SE/8');
    })
    .toArray()
    .subscribe(episodes => {
        console.log(episodes);
    });

但是当使用toArray时,它不再起作用,我不会订阅任何内容:/

2 个答案:

答案 0 :(得分:1)

您的问题是$nummer = $_POST['param1']; $komm = $_POST['param2']; $entnommen = $_POST['param3']; $url = 'https://sheets.googleapis.com/v4/spreadsheets/1TDkPMU9EzLwV45V1CrmNU4Xscc3GH9dKc1HDTIzc16k/values/Test!A1:append?includeValuesInResponse=true&insertDataOption=INSERT_ROWS&valueInputOption=RAW'; $ch = curl_init(); $data = array("range" => "Test!A1", "majorDimension" => "ROWS", "values" => array(array($nummer,$komm,$entnommen))); $data_string = json_encode($data); $headr = array(); $headr[] = 'Content-type: application/json'; $headr[] = 'Authorization: Bearer '.$token; $headr[] = 'Accept: application/json'; curl_setopt_array($ch, array( CURLOPT_HTTPHEADER => $headr, CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $url, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $data_string )); //execute post $result = curl_exec($ch); $statusCode = curl_getInfo($ch, CURLINFO_RESPONSE_CODE); var_dump($result); //close connection curl_close($ch); $character = json_decode($result); 是一个事件发射器。我假设每次收集更改时它都会发出一个事件。总是会有更多更改,因此valueChanges永远不会调用valueChanges,而complete会永远等待。

您无法在此处使用toArray。必须订阅observable并处理每个条目。或者您可以使用toArray并自行将项目添加到阵列中。对于推送到源可观察对象的每个新值,只调用reduce,因此每次通过过滤器的更改都会更新

答案 1 :(得分:0)

也许这是一个迟来的答案:)
但简单的解决方法可以是:

this.getEpisodesForStoryEvents()
      .switchMap(episodes => {
        return Observable.from(episodes))
      .filter((episode: Episode) => {
        return episode.ref.startsWith('SE/8');
      })
      .toArray()
       }
  .subscribe(episodes => {
      console.log(episodes);
    });

所以我只是将filter和toArray封装在switchMap中