Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / November 2007



Tip: Looking for answers? Try searching our database.

javascript array syntax

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Giakko - 30 Nov 2007 10:48 GMT
hello,
is there a way to create an array in this way?:

new Array('key1'='value1','key2'='value2','key3'='value3')   ??????

thanks,

simone,
http://www.notte.com
Bart Van der Donck - 30 Nov 2007 11:59 GMT
> is there a way to create an array in this way?:
>
> new Array('key1'='value1','key2'='value2','key3'='value3')

This type of construction would not be an array anymore (though some
call it an associative array). The preferred term in javascript is an
object.

 { key1: 'value1', key2: 'value2', key3: 'value3' }

--
Bart
Randy Webb - 30 Nov 2007 14:21 GMT
Bart Van der Donck said the following on 11/30/2007 6:59 AM:

>> is there a way to create an array in this way?:
>>
[quoted text clipped - 5 lines]
>
>   { key1: 'value1', key2: 'value2', key3: 'value3' }

What you created is an Object, and I am not sure that is what the OP was
asking about. It reads as if it is being asked for a shortcut method to
doing this:

var myArray = new Array()
myArray['key1'] = 'value1';
myArray['key2'] = 'value2';
myArray['key3'] = 'value3';

myArray is still an Array, it is just overkill when a plain Object would
suffice. Even then, it is still an "Object", just an extended Object
with Array properties.

Signature

Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

VK - 30 Nov 2007 15:15 GMT
> Bart Van der Donck said the following on 11/30/2007 6:59 AM:
>
[quoted text clipped - 20 lines]
> suffice. Even then, it is still an "Object", just an extended Object
> with Array properties.

Date is also an extended Object with extra properties. The moment any
programmers gets the *difference* between an Object and an Array is
the moment one stops perling/phping around and starts programming.
Don't tell me you didn't reach this state, please.
Thomas 'PointedEars' Lahn - 30 Nov 2007 18:03 GMT
>> var myArray = new Array()
>> myArray['key1'] = 'value1';
[quoted text clipped - 6 lines]
>
> Date is also an extended Object with extra properties.

Date is a *built-in* object that only *inherits from* Object.prototype
through the prototype chain.

> The moment any programmers gets the *difference* between an Object and an
> Array is the moment one stops perling/phping around and starts programming.
> Don't tell me you didn't reach this state, please.

Google Groups knows that so far you never got any difference between
anything here (and the prospects for a change of that are equally bad),
so that point really is moot.

PointedEars
Signature

   realism:    HTML 4.01 Strict
   evangelism: XHTML 1.0 Strict
   madness:    XHTML 1.1 as application/xhtml+xml
                                                   -- Bjoern Hoehrmann

VK - 30 Nov 2007 18:49 GMT
On Nov 30, 9:03 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> >> var myArray = new Array()
> >> myArray['key1'] = 'value1';
[quoted text clipped - 8 lines]
>
> Date is a *built-in* object that only *inherits from* Object.prototype

So is String, Number, Boolean or Array or so on. Listen: you learn the
basics of OOP or stop posting. It's getting annoying.
Thomas 'PointedEars' Lahn - 30 Nov 2007 19:03 GMT
>>>> var myArray = new Array() myArray['key1'] = 'value1';
>>>> myArray['key2'] = 'value2'; myArray['key3'] = 'value3'; myArray is
[quoted text clipped - 5 lines]
>
> So is String, Number, Boolean or Array or so on.

But `key1', `key2' and `key3' are _not_ properties that are *inherited* from
another (built-in) object, and newly created object is _not_ a *built-in*
object.  That is the distinction you fail to make here.

> Listen: you learn the basics of OOP or stop posting. It's getting
> annoying.

ROTFL

PointedEars
Signature

Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Randy Webb - 30 Nov 2007 23:55 GMT
VK said the following on 11/30/2007 10:15 AM:
>> Bart Van der Donck said the following on 11/30/2007 6:59 AM:
>>
[quoted text clipped - 18 lines]
>
> Date is also an extended Object with extra properties.

I would ask you what you think that has to do with what I wrote, but I
don't think I want to entertain the wild thought processes you have at
times.

> The moment any programmers gets the *difference* between an Object
> and an Array is the moment one stops perling/phping around and
> starts programming.

Your village misses you badly.

> Don't tell me you didn't reach this state, please.

I will be completely relieved if, by your reasoning, if you would at
least try to attain the status of "perling/phping around" and leave JS
alone until after that time. Then maybe you can "start programming".
Until then, your village misses you badly.

Signature

Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Gregor Kofler - 30 Nov 2007 13:49 GMT
Giakko meinte:
> hello,
> is there a way to create an array in this way?:
>
> new Array('key1'='value1','key2'='value2','key3'='value3')   ??????

JS doesn't know associative arrays. As Bart said - use an object instead.

Gregor

Signature

http://www.gregorkofler.at ::: Landschafts- und Reisefotografie
http://www.licht-blick.at  ::: Forum für Multivisionsvorträge
http://www.image2d.com     ::: Bildagentur für den alpinen Raum

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.