473,224 Members | 1,324 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,224 software developers and data experts.

quick question about labelling array elements

Im a php coder - not really used to javascript. Can someone please
tell me how to assign labels to array elements and then call them by
the index?

var mycars = new Array(3)
mycars['bigone'] = "Saab"
mycars['biggerone'] = "Volvo"
mycars[2] = "BMW"

document.write(mycars[0]);

is printing nothing when in php the same idea would spit out "Saab"

Cheers,
Ciarán

Mar 27 '07 #1
6 1509
On Mar 26, 7:09 pm, "Ciaran" <cronok...@hotmail.comwrote:
Im a php coder - not really used to javascript. Can someone please
tell me how to assign labels to array elements
use object

mycars={}
mycars['bigone']="Saab"
....
and then call them by
the index?
not possible with object

Mar 27 '07 #2
On Mar 27, 2:09 am, "Ciaran" <cronok...@hotmail.comwrote:
Im a php coder - not really used to javascript. Can someone please
tell me how to assign labels to array elements and then call them by
the index?

var mycars = new Array(3)
mycars['bigone'] = "Saab"
mycars['biggerone'] = "Volvo"
mycars[2] = "BMW"

document.write(mycars[0]);

is printing nothing when in php the same idea would spit out "Saab"
Never Mind I found a solution:
for (x in mycars)
{
document.write(mycars[x])
}

Thanks anyway guys!
Ciarán


Mar 27 '07 #3
"Ciaran" <cr*******@hotmail.comwrote in message
news:11*********************@d57g2000hsg.googlegro ups.com...
Im a php coder - not really used to javascript. Can someone please
tell me how to assign labels to array elements and then call them by
the index?
var mycars = new Array(3)
mycars['bigone'] = "Saab"
mycars['biggerone'] = "Volvo"
mycars[2] = "BMW"
document.write(mycars[0]);
is printing nothing when in php the same idea would spit out "Saab"
That is because your first declare it as an "associative" array, which, whilst it does not
exist in JavaScript, is a way to access the values. I may be wording this incorrectly, so
I will give an example.

arr1['author'] = 'Stephen King';
arr1.push('0th');

Now, if you do:

alert(arr1[0]);

.... you get 0th. Only arr1['author'] would retrieve what you expect.

The best method of doing what you want (entirely associative) is to use JSON on an Object.

var parents = {};
parents = {
'daddy' : 'The Dad',
'mommy' : 'The Mom'
}

This actually led me to write a routine that took an indice and a value and wrote to the
array accordingly (thereby giving me what you know as an associative array).

-Lost
Mar 27 '07 #4
>"Ciaran" <cr*******@hotmail.comwrote in message
>news:11**********************@n59g2000hsh.googleg roups.com...
On Mar 27, 2:09 am, "Ciaran" <cronok...@hotmail.comwrote:
>Im a php coder - not really used to javascript. Can someone please
tell me how to assign labels to array elements and then call them by
the index?

var mycars = new Array(3)
mycars['bigone'] = "Saab"
mycars['biggerone'] = "Volvo"
mycars[2] = "BMW"

document.write(mycars[0]);

is printing nothing when in php the same idea would spit out "Saab"
>Never Mind I found a solution:
for (x in mycars)
{
document.write(mycars[x])
}
Alright, even though, baring in mind, that is *not* what you asked for. If you had said
how to I retrieve all of my elements in the array, I would have given you the for-in loop.
Thanks anyway guys!
You are most welcome.

-Lost
Mar 27 '07 #5
On Mar 27, 2:55 am, "-Lost" <missed-s...@comcast.netwrote:
var parents = {};
parents = {
'daddy' : 'The Dad',
'mommy' : 'The Mom'

}
Thanks a lot Lost, This explains a lot and is much clearer than the
average posts on this topic. Is there a way to call the 'mommy' using
an index by chance?

Ciarán

Mar 27 '07 #6
Lee
Ciaran said:
>
On Mar 27, 2:55 am, "-Lost" <missed-s...@comcast.netwrote:
>var parents =3D {};
parents =3D {
'daddy' : 'The Dad',
'mommy' : 'The Mom'

}

Thanks a lot Lost, This explains a lot and is much clearer than the
average posts on this topic. Is there a way to call the 'mommy' using
an index by chance?
No, because parents['mommy'] is not actually an element of the array.
It is a new attribute that has been added to the Array object.

"square bracket" notation has two completely different meanings,
depending on whether the brackets contain an integer or a string.
They both look like they access array elements, but that's not
really what's going on:

parents[integer] sets or gets the array element at position integer
parents[string] sets or gets the object attribute named by string
--

Mar 27 '07 #7

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

Similar topics

3
by: Mike Cain | last post by:
I have an odd situation I'm trying to understand..... I'm using MS VS 7.0 C++ with the MFC CBuffer class. If I do this: #define NUM_ELEMENTS 2 CBuffer<char, 512 x; CBuffer<char, 512 ...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.