Arrays 如何读取对象并存储到数组中

Arrays 如何读取对象并存储到数组中,arrays,json,angular,Arrays,Json,Angular,我发出了一个http请求并返回,例如下面的json。我希望在http请求时将cars和shoes对象读入单独的数组 json { "id": 9, "name": "Zlatan", "cars": [ { "type": "ford", "year": "2019" }, { "type": "audi", "year": "2017" } ] "shoes": [ { "brand"

我发出了一个http请求并返回,例如下面的json。我希望在http请求时将cars和shoes对象读入单独的数组

json

{
 "id": 9, 
 "name": "Zlatan", 
 "cars": [
    {
     "type": "ford", 
     "year": "2019"
    },
    {
     "type": "audi", 
     "year": "2017"
    }
  ]
 "shoes": [
    {
     "brand": "adidas", 
     "status": "old"
    },
    {
     "brand": "timberland", 
     "status": "new"
    }
  ]
}
comp.ts

cars = [];
shoes = [];
//......

getZlatan() {
   return this.httpService.getInfo()
         .subscribe( data => {
                this.objZlatan = data;   //this part holds the json obj above
                this.cars ....//what to add here
                this.shoes ....// here too. 
            } );
}

或者是否有一种更干净的方法可以根据http请求加载数组?

使用简单的点符号访问数据的
cars
shoes
属性。最好使用
if(此处的条件)
检查返回的数据是否不为null,然后执行逻辑。如果您有更多的对象,并且希望将所有的汽车和鞋子放在一个阵列下,那么您必须循环通过

getZlatan() {
    return this.httpService.getInfo()
        .subscribe(data => {
            this.objZlatan = data;
            this.cars = this.objZlatan.cars;
            this.shoes = this.objZlatan.shoes; 
        });
}

只需使用
并键入名称即可访问
汽车
。让我举一个例子:

return this.httpService.getInfo()
     .subscribe( data => {
            this.objZlatan = data;   //this part holds the json obj above
            this.cars = data.cars;
            this.shoes =data.shoes;
        } );

this.cars=data.cars;this.shoes=data.shoes。这不管用吗?
this.cars=data.cars
this.shoes=data.shoes
,等等。