quick question about labelling array elements

C

Ciaran

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
 
S

scripts.contact

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
 
C

Ciaran

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
 
L

-Lost

Ciaran said:
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
 
L

-Lost

Ciaran said:
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
 
C

Ciaran

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
 
L

Lee

Ciaran said:
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


--
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top