首页 > 解决方案 > 如何将任何 [] 转换为可观察的在角度 6

问题描述

我有一个countryList : any[]具有以下数据的变量:

[
  {"countrycode":"SH","countryname":"Saint Helena"},
  {"countrycode":"KN","countryname":"Saint Kitts and Nevis"},   
  {"countrycode":"LC","countryname":"Saint Lucia"},   
  {"countrycode":"IS","countryname":"Iceland"}, 
  {"countrycode":"IN","countryname":"India"},  
  {"countrycode":"ID","countryname":"Indonesia"},  
  {"countrycode":"IR","countryname":"Iran (Islamic Republic of)"},   
  {"countrycode":"IQ","countryname":"Iraq"},    
  {"countrycode":"IE","countryname":"Ireland"},    
  {"countrycode":"IM","countryname":"Isle of Man"}, 
  {"countrycode":"IL","countryname":"Israel"}, 
  {"countrycode":"SH","countryname":"Saint Helena"},
  {"countrycode":"KN","countryname":"Saint Kitts and Nevis"}, 
  {"countrycode":"LC","countryname":"Saint Lucia"},
  {"countrycode":"ZM","countryname":"Zambia"},
  {"countrycode":"ZW","countryname":"Zimbabwe"}
]

我想将其转换为可观察的

这该怎么做 ?

标签: angularangular5angular6

解决方案


如果您使用的是 rxjs 6,您可以简单地使用:

import { of } from 'rxjs';

// ...

let obs = of([
     {"countrycode":"SH","countryname":"Saint Helena"},
     {"countrycode":"KN","countryname":"Saint Kitts and Nevis"},
     // etc... 
])

如果您使用的是旧版本,请替换ofObservable.of


推荐阅读