Associative array...help!

K

kaipiroz

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?

Thanx in advance,
Augusto
 
R

Robin

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
 
K

kaipiroz

Thanx Robin so much!

Now I only need to understand how to correctly build the variable
server-side (before JSON encoding it).

Augusto


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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top