JSON响应中的Angular 2过滤器

JSON响应中的Angular 2过滤器,json,angular,typescript,Json,Angular,Typescript,我无法从JSON响应中筛选数据。我正在使用谷歌地理编码API,并得到回应,我只想返回城市名称。城市名称存储在其中一个数组的JSON响应中,其中types=“locality”,“political” 如何检索城市名称,其中类型=[“地区”、“政治”] 这是我的密码: 地理编码.service.ts getLocation(lat: number, lon: number): Promise<any> { return this.http.get(this.apiUrl +

我无法从JSON响应中筛选数据。我正在使用谷歌地理编码API,并得到回应,我只想返回城市名称。城市名称存储在其中一个数组的JSON响应中,其中
types=“locality”,“political”

如何检索城市名称,其中
类型=[“地区”、“政治”]

这是我的密码:

地理编码.service.ts

 getLocation(lat: number, lon: number): Promise<any> {
    return this.http.get(this.apiUrl + lat + ',' + lon + '&key' + this.apiKey)
      .toPromise()
      .then((response) => Promise.resolve(response.json()));
  }
export class Geocoding {
    results: {
        address_components: {
            long_name: string;
            short_name: string;
        }
        formatted_address: string;
        types: {
            array: string;
            length: number;
        }
    }
    status: string;
}
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { GeocodingService } from '../data/google/geocoding/geocoding.service';
import { Geocoding } from '../data/google/geocoding/geocoding';

@Component({
  selector: 'app-weather',
  templateUrl: './weather.component.html',
  styleUrls: ['./weather.component.scss']
})

  location: Geocoding[];
  datasource: Geocoding[];
  sortedList: Geocoding[];

 ngOnInit() {
    this.findLocation2(52.406374, 16.9251681);
  }

 findLocation2(latitude: number, longtitude: number) {
    this.GeoService.getLocation(latitude, longtitude).then(data => {
      this.datasource = data;
      this.location = this.datasource;
      this.sortedList = this.location.filter(
        loc => loc.results.types.array === 'locality,political');
    });
weather.component.ts

 getLocation(lat: number, lon: number): Promise<any> {
    return this.http.get(this.apiUrl + lat + ',' + lon + '&key' + this.apiKey)
      .toPromise()
      .then((response) => Promise.resolve(response.json()));
  }
export class Geocoding {
    results: {
        address_components: {
            long_name: string;
            short_name: string;
        }
        formatted_address: string;
        types: {
            array: string;
            length: number;
        }
    }
    status: string;
}
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { GeocodingService } from '../data/google/geocoding/geocoding.service';
import { Geocoding } from '../data/google/geocoding/geocoding';

@Component({
  selector: 'app-weather',
  templateUrl: './weather.component.html',
  styleUrls: ['./weather.component.scss']
})

  location: Geocoding[];
  datasource: Geocoding[];
  sortedList: Geocoding[];

 ngOnInit() {
    this.findLocation2(52.406374, 16.9251681);
  }

 findLocation2(latitude: number, longtitude: number) {
    this.GeoService.getLocation(latitude, longtitude).then(data => {
      this.datasource = data;
      this.location = this.datasource;
      this.sortedList = this.location.filter(
        loc => loc.results.types.array === 'locality,political');
    });
当我运行时,我得到一个错误:

异常:未捕获(承诺中):TypeError:\u this.location.filter不是函数


this.location
is应该是一个数组。因此,在执行
筛选之前,需要检查
this.location
是否为数组

findLocation2(latitude: number, longtitude: number) {
    this.GeoService.getLocation(latitude, longtitude).then(data => {
      this.datasource = data;
      this.location = this.datasource;
      if(Array.isArray(this.location)){
      this.sortedList = this.location.filter(
        loc => loc.results.types.array === 'locality,political');
    });
};

我找到了解决办法!@Ramesh Rajendran和@Sebastian都为我指出了正确的方向:

 findLocation(latitude: number, longtitude: number) {
    return this.GeoService.getData(latitude, longtitude).subscribe(g => {
      this.geo = g;
      this.GEOARRAY = g.results;
      if (isArray(this.GEOARRAY)) {
        this.location = this.GEOARRAY.filter(x => x.types[0] === "locality" && x.types[1] === "political");
      }
    });
  }

我创建了
GEOARRAY
,并指向
g.results
(即数组)。然后我过滤了我想要的:)谢谢

关于这个问题有很多问题。您只能从服务中获得纯JSON,而不是带有函数的JS对象。你需要有一个构造函数,它接受JSON并创建一个可行的对象。我找到了解决方案,请在下面查找Hanks,错误是固定的,但是当我想要
console.log(this.sortedList)时
它是未定义的,因为
这个位置不是数组。因此,您应该检查
getLocation
是否为返回数组。我找到了解决方案,请在下面查找