Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <2453001.kcvsFY6v6J@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Thu, 12 Jan 2012 00:26:02 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit Subject: Re: Array syntax Newsgroups: comp.lang.javascript References: <8bcdc6d4-cf3b-4af3-901d-8464de4de2f2@k10g2000yqk.googlegroups.com> <4f0c025c$0$30150$a8266bb1@newsreader.readnews.com> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 67 NNTP-Posting-Date: 12 Jan 2012 00:26:03 CET NNTP-Posting-Host: c1932768.newsspool3.arcor-online.net X-Trace: DXC=b`9bc< On Mon, 09 Jan 2012 14:11:11 -0800, DrKen wrote: > > var locations = $("#field-location select:first option"); > var loc = new Array(); > loc["az"] = { value: "AZ", caption: "Azusa" }; > loc["co"] = { value: "CO", caption: "Colorado Springs" }; > loc["hd"] = { value: "VI", caption: "High Desert Regional Center" }; > loc["ie"] = { value: "IE", caption: "Inland Empire Regional Center" }; > loc["in"] = { value: "IN", caption: "International" }; > loc["la"] = { value: "LA", caption: "Los Angeles Regional Center" }; > loc["mu"] = { value: "MU", caption: "Murrieta Regional Center" }; (sic!) Is this your code or are you quoting the OP? Watch for the quotation levels. It does not make a lot of sense to use an `Array' instance here. The properties of `Array' instances are not employed here. The array data structure is _not_ added elements with indexes "az", "co" etc., but the `Array' instance is *augmented* with *properties* named "az", "co" etc. As a result, the value of `loc.length' remains 0 (evidence that there are no built-in associative arrays in ECMAScript implementations). Indeed, should for some reason a *perceived* "index" be the name of a built-in property of `Array' instances or their prototype, unexpected behavior would be the result. For example: /* RangeError: Invalid array length */ loc["length"] = { value: "AZ", caption: "Azusa" }; (because there is no Object.prototype.toValue() and Object.prototype.toString() returns "[object Object]" for `Object' instances, which is numerically converted to NaN.) So it should be var log = new Object(); or more simple var log = {}; (as already used) instead. However, it is not very efficient to add properties on initialization that way. As already used, properties can be defined in the Object initializer/literal, and initializers can be nested: var log = { az: { value: "AZ", caption: "Azusa" }, … }; Obviously, if the data were generated by the server, a conversion of the server-side data to JSON (for example, from PHP with json_encode()) would be the simplest way to make it available to the client-side code (JSON differs here only in that it requires the property name to be quoted, which is also valid ECMAScript). PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from (404-comp.)