I'm getting an array of data from res.persons
within my API call and storing it in the people
array. When I console.log(this.people)
I can see the elements but when I try to console.log(this.people.length)
OR console.log(this.people[0])
I get undefined.
people = [];
pushAll(arr){
for(var i = 0; i < arr.length; i++){
this.people.push(arr[i]);
}
}
Here's the snippet of my component where the API call is being made.
ngOnInit() {
this._http.get(this._url, options)
.map((res: Response) => res.json())
.subscribe(res => this.pushAll(res.persons));
}
This is output of console.log(this.people)
:
This has been driving me crazy, so any help will be appreciated!