基本上我有一个角度分量,它正在从WEBApi处获得响应,如下所示
export class FetchDataComponent implements OnInit {
constructor(private _httpService: Http) { }
City: string[] = [];
ngOnInit() {
// city and country
this._httpService.get('/api/City').subscribe(values => {
this.City = values.json() as string[];
});
}
}
我在这里收到的回复是[“ Waterloo”,“ Canada”]
如何从以上回复中提取城市滑铁卢?根据要求,我需要在这里做。
我试图将滑铁卢存储在单独的变量中,将加拿大存储在单独的变量中。
答案 0 :(得分:0)
我不确定我是否理解你的问题。如果您想从City
对象中获取“滑铁卢”,则只需使用Array.find
var item = this.City.find(x => x == 'Waterloo');
答案 1 :(得分:0)
响应为字符串数组:
City: string[] = [];
var1: string;
var2: string;
ngOnInit() {
this._httpService.get('/api/City').subscribe(values => {
this.City = values.json() as string[];
this.var1 = this.City[0];
this.var2 = this.City[1];
});
}
答案 2 :(得分:0)
City: string[] = [];
ngOnInit() {
this._httpService.get('/api/City').subscribe(values => {
this.City = values.json() as string[];
var city = this.City.find(x => x === 'Waterloo'(please input city you want to get));
});
}