467,161 Members | 915 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

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
  • viewed: 1325
Share:
6 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.