Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > quick question about labelling array elements

Reply
Thread Tools

quick question about labelling array elements

 
 
Ciaran
Guest
Posts: n/a
 
      03-27-2007
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

 
Reply With Quote
 
 
 
 
scripts.contact
Guest
Posts: n/a
 
      03-27-2007
On Mar 26, 7:09 pm, "Ciaran" <cronok...@hotmail.com> wrote:
> 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

 
Reply With Quote
 
 
 
 
Ciaran
Guest
Posts: n/a
 
      03-27-2007
On Mar 27, 2:09 am, "Ciaran" <cronok...@hotmail.com> wrote:
> 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




 
Reply With Quote
 
-Lost
Guest
Posts: n/a
 
      03-27-2007
> "Ciaran" <> wrote in message
> news: 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


 
Reply With Quote
 
-Lost
Guest
Posts: n/a
 
      03-27-2007
>> "Ciaran" <> wrote in message
>> news: oups.com...

On Mar 27, 2:09 am, "Ciaran" <cronok...@hotmail.com> wrote:
>> 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


 
Reply With Quote
 
Ciaran
Guest
Posts: n/a
 
      03-27-2007
On Mar 27, 2:55 am, "-Lost" <missed-s...@comcast.net> wrote:

> 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

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      03-27-2007
Ciaran said:
>
>On Mar 27, 2:55 am, "-Lost" <missed-s...@comcast.net> wrote:
>
>> 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


--

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CGI checkbox labelling confuses javascript Greg G Perl 2 07-12-2006 02:38 PM
Cd Labelling software Pete Brown Computer Support 7 11-12-2005 09:37 AM
Labelling prints in galleries: "photo" vs "digital photo" vs "digital manipulation"... Alan Justice Digital Photography 2 06-08-2005 03:10 PM
Pie Chart - Labelling =?Utf-8?B?cmtibmFpcg==?= ASP .Net 1 01-21-2005 08:57 PM
Labelling tapes Greg M Computer Support 31 12-25-2003 12:25 AM



Advertisments