Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从订阅访问数据_Javascript_Json_Angular - Fatal编程技术网

Javascript 从订阅访问数据

Javascript 从订阅访问数据,javascript,json,angular,Javascript,Json,Angular,我正在学习Angular 2,已经订阅了我的服务并返回了数据,但是我现在需要for()在它进入视图之前对它进行循环,所以我将我的json推到一个已经是数组的数组中,但不确定这是否正确 基本上,我希望将订阅的数据传递给控制器上的方法 public sublocation = []; private subscriptions = []; constructor(private statusService : StatusService) { } public getAll

我正在学习Angular 2,已经订阅了我的服务并返回了数据,但是我现在需要
for()
在它进入视图之前对它进行循环,所以我将我的
json
推到一个已经是数组的数组中,但不确定这是否正确

基本上,我希望将订阅的数据传递给控制器上的方法

public sublocation = [];
  private subscriptions = [];
  constructor(private statusService : StatusService) { 

  }  

  public getAllStatusList(){
    this.subscriptions.push(
      this.statusService.getStatusAPI()
      .subscribe(
        data => {
            this.sublocation.push(data);
        },
        error => console.log('Server Error'), 
      )
    );
  }

public getSublocation(){
    console.log(this.sublocation);
    //TRYING TO DO STUFF HERE like if sublocation == london
}
json示例

[
  [
    {
      "id": "19",
      "parentTag": "0",
      "name": "London",
      "deliveryType": "E",
      "enabled": "T",
      "outageTag": [],
      "sub": [
        {
          "id": "25",
          "parentTag": "19",
          "name": "London::Gatwick",
          "deliveryType": "E",
          "enabled": "T",
          "outageTag": []
        }
      ]
    }
  ]
]
 export class StatusService {

  constructor(private http: Http) { 

  }

  public getStatusAPI(): Observable<any>{

    return this.http.get(environment.api)
      .map((response: Response) => this.getStatusList(response))
      .catch((error:any) => Observable.throw(error.json().error || 'Server Error'));
  }

  public getStatusList(responseData:any){
    let data = [];
    let res = responseData.json();
    for(let prop in res) {
      data.push(res[prop])
    }
    //console.log(data);
    return data;
  }

}
服务

[
  [
    {
      "id": "19",
      "parentTag": "0",
      "name": "London",
      "deliveryType": "E",
      "enabled": "T",
      "outageTag": [],
      "sub": [
        {
          "id": "25",
          "parentTag": "19",
          "name": "London::Gatwick",
          "deliveryType": "E",
          "enabled": "T",
          "outageTag": []
        }
      ]
    }
  ]
]
 export class StatusService {

  constructor(private http: Http) { 

  }

  public getStatusAPI(): Observable<any>{

    return this.http.get(environment.api)
      .map((response: Response) => this.getStatusList(response))
      .catch((error:any) => Observable.throw(error.json().error || 'Server Error'));
  }

  public getStatusList(responseData:any){
    let data = [];
    let res = responseData.json();
    for(let prop in res) {
      data.push(res[prop])
    }
    //console.log(data);
    return data;
  }

}
导出类状态服务{
构造函数(专用http:http){
}
public getStatusAPI():可观察{
返回此.http.get(environment.api)
.map((响应:响应)=>this.getStatusList(响应))
.catch((error:any)=>Observable.throw(error.json().error | |“服务器错误”);
}
公共getStatusList(响应数据:任意){
让数据=[];
让res=responseData.json();
for(让道具进入res){
数据推送(res[prop])
}
//控制台日志(数据);
返回数据;
}
}

我假设您在问题中以JSON(不再是JSON)的形式呈现的数据是在循环并将其推送到数组后最终得到的数据。因此,原始(和实际)JSON可能如下所示:

[
  {
    "id": "1",
    "parentTag": "0",
    "name": "London",
    "deliveryType": "E",
    "enabled": "T",
    "outageTag": [],
    "sub": [
      {
        "id": "25",
        "parentTag": "19",
        "name": "London::Gatwick",
        "deliveryType": "E",
        "enabled": "T",
        "outageTag": []
      }
    ]
  }
]
this.statusService.getStatusAPI()
  .subscribe(d => {
    this.data = d;
    this.loopData();
  })

loopData() {
  // loop through array
  this.data.forEach(x => {
    // check name property of object
    if(x.name == 'London') {
      alert('An Object with name London found!');
    }
  });
}
这意味着不需要循环和推送到新阵列,您可以这样做:

return this.http.get(environment.api)
  .map(res => res.json())
在您的组件中,订阅数据并执行任何您喜欢的操作。您在问题中提到,您希望在阵列中循环并检查位置。因此,请这样做:

[
  {
    "id": "1",
    "parentTag": "0",
    "name": "London",
    "deliveryType": "E",
    "enabled": "T",
    "outageTag": [],
    "sub": [
      {
        "id": "25",
        "parentTag": "19",
        "name": "London::Gatwick",
        "deliveryType": "E",
        "enabled": "T",
        "outageTag": []
      }
    ]
  }
]
this.statusService.getStatusAPI()
  .subscribe(d => {
    this.data = d;
    this.loopData();
  })

loopData() {
  // loop through array
  this.data.forEach(x => {
    // check name property of object
    if(x.name == 'London') {
      alert('An Object with name London found!');
    }
  });
}

使用此解决方案,您现在可以轻松访问对象数组中的数据,如上所述,在数组中循环,然后引用对象属性,如
x.name
x.parentTag
等。

这是无效的json,数组[]需要有对象{},数组中有数组,然后{}请分享服务如何准确返回数据,以及您需要如何处理这些数据exactly@SumamaWaheed数组不需要有对象。数组可以嵌套<代码>[[2]]有效JSON@ottz0,您能否在任何循环之前发布json到达时的确切外观