-1

I have some data being returned in a JSON Dataset as follows in the Console.Log:

Looks like it's an array of JSON entries? To try to access this data, i've been using this type of JSON loop:

console.log(data);
for (var key in data) {
    if (data.hasOwnProperty(key)) {
        console.log(data[key].Station);
    }
}

But it's constantly coming back as "Undefined". Why? And how do I collect this data via iteration?

Rich
  • 4,134
  • 3
  • 26
  • 45
  • `key[keys]` wat, what is `keys`? and why would you use it to access a property of a string? – Kevin B Oct 16 '17 at 22:40
  • My bad. I’ll fix that shortly. Even as data[keys] it still doesn’t work. – Rich Oct 16 '17 at 22:42
  • `console.log(data[key].Station);` would work, but you are working with an array, so `key` will be `0`, then `1` etc. Perhaps try just `data.map(console.log)` (without any loop) ? – Alex McMillan Oct 16 '17 at 22:48
  • `data` is an object with a single property `tags` whose value is an array. Seems like you want `data.tags.forEach(o => console.log(o.Station))`. *"But it's constantly coming back as "Undefined". Why?"* Because you are trying to access `data.tags.Station` which doesn't exist. – Felix Kling Oct 16 '17 at 22:51
  • Yeah, the guy that answered it below had the right answer. Thanks guys. – Rich Oct 16 '17 at 22:52

1 Answers1

0

I think you are not looping the correct array. Your data is a object, not the array you would like to loop.

Here I try to simulate your data and loop through it, maybe it can help you.

var data = {};
data.tags = [
  {id:1, name:"John"},
   {id:2, name:"Alice"},
   {id:3, name:"Some monky"}
];

console.log(data);

for ( var i=0; i < data.tags.length; i++ ) {
  console.log(data.tags[i].name);
}
user8672473
  • 236
  • 2
  • 8