Reputation: 1015
So I have the http.get call working and can correctly grab data from the request:
this.http.get('https://data.cityofnewyork.us/resource/xx67-kt59.json').subscribe(data => {
// Read the result field from the JSON response.
console.log(data);
for (let x in data) {
console.log(x);
}
});
The console.log(data) line works successfully, it prints me out an array of th e items from the request.
The trouble I'm having is when I try and iterate through each individually item in the JSON array.
for (let x in data) {
console.log(x);
}
Just prints me out the index/number of the item in the JSON object. Even if I try x["prop name"], it still fails.
Even the following doesn't work:
console.log(JSON.stringify(x));
My goal is to eventually map these to a class I'd define and populate an array variable with that data.
Am I being stupid in how I'd reference the JSON property for each item?
Upvotes: 1
Views: 138
Reputation: 8836
If you want to iterate over an Array
you should use the for of
construct.
for (let x of data) {
// `x` is an item from the array
}
The for in
construct iterates over the keys of an object. In your case those are the array indexes.
Upvotes: 2
Reputation: 412
Try using for (let x of data)
instead of for (let x in data)
.
Upvotes: 0