无法显示存储在数组中的JSON响应

时间:2017-05-03 14:27:26

标签: javascript arrays api angular

我在API调用中从res.persons获取数据并将其存储在people数组中。当我console.log(this.people)时,我可以看到元素,但当我尝试console.log(this.people.length)console.log(this.people[0])时,我得到了未定义。

people = [];

pushAll(arr){
      for(var i = 0; i < arr.length; i++){
        this.people.push(arr[i]);
      }
 }

这是我正在进行API调用的组件片段。

ngOnInit() {
    this._http.get(this._url, options)
            .map((res: Response) => res.json())
            .subscribe(res => this.pushAll(res.persons));
}

这是console.log(this.people)

的输出

console screenshot

这让我发疯,所以任何帮助都会受到赞赏!

0 个答案:

没有答案