ka******@gmail.com wrote:
Hi all,
I have this js array dynamically generated by the server and converted
to JSON format:
var punti = [{"3": {"online": "no", "username": "phishman"}}, {"4":
{"online": "no", "username": "marco"}}]
and I need to catch the online status for every user but :
for (prop in punti) {
console.log(punti[prop].online);
}
returns "undefined" for every item in the array.
Why?
Showing your array as a tree structure may help:
punti = [
{
'3' : {
'online' : 'no',
'username' : 'phishman'
}
},
{
'4' : {
'online' : 'no',
'username' : 'marco'
}
}
]
So, first time through the loop, (prop being index 0)
punti[prop] = {
'3' : {
'online' : 'no',
'username' : 'phishman'
}
}
i.e. it only has a '3' property not an 'online' property.
Your array needs to be either:
var punti = {"3": {"online": "no", "username": "phishman"},
"4":{"online": "no", "username": "marco"}}
or
var punti = [{"num": "3", "online": "no", "username": "phishman"},
{"num": "4", "online": "no", "username": "marco"}];
depending upon what the numbers 3 and 4 are for.
Robin