473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Associative array...help!

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

Dec 13 '07 #1
3 1214
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
Dec 13 '07 #2
Thanx Robin so much!

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

Augusto
On Dec 13, 1:08 pm, Robin <a...@somewhere.comwrote:
kaipi...@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
Dec 13 '07 #3
ka******@gmail.com wrote:
Now I only need to understand how to correctly build the variable
server-side (before JSON encoding it).
[...]
[Top post]
And how to quote properly in Usenet. http://jibbering.com/faq/
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Dec 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.