[PLUG] Javascript Array Problem

Carlos Konstanski ckonstan at lunarlogic.com
Wed Sep 14 20:49:54 UTC 2005


Javascript arrays are sparse.  They're not really numerical-indexed
arrays, they're more like associative arrays.  It's still good form to
start them at 0, but it's not a bug to start at 1.

I believe function MakeArray() is unnecessary.  You do not have to
pre-pad an array in javascript.  Just use:

     var newArray = new Array();
     newArray[1] = "the value of 1";
     newArray[37] = "the value of 37";

Note the large skip in indices.  Perfectly legal in javascript.  This is
what I mean by sparse.

Or you could opt for a real associative array, with strings for keys:

     var newAlist = new Object();
     newAlist["one"] = "the value of one";
     newAlist["thirtyseven"] = "the value of thirtyseven";

though this has nothing to do with the current problem.  Just an
observation to help unserstanding of js arrays.  an Array() is an
associative array with numbers for keys, though they can be any numbers,
and not even consecutive.  An Object() can be used as a string-keyed
associative array.

I still say all bets are off until some semicolons get put in.

On Wed, 14 Sep 2005, Bill Ensley wrote:

> Date: Wed, 14 Sep 2005 13:14:08 -0700
> From: Bill Ensley <bill at bearprinting.com>
> Reply-To: "General Linux/UNIX discussion and help; civil and on-topic"
>     <plug at lists.pdxlinux.org>
> To: "'General Linux/UNIX discussion and help; civil and on-topic'"
>     <plug at lists.pdxlinux.org>
> Subject: RE: [PLUG] Javascript Array Problem
> 
> My javascript is rusty, but shouldn't this array start at 0?
>
> //======================================================================
> function buildPortlandArray() {
>   portlandArray[1] = new img("P0001", "Oregon City Bridge")
>   portlandArray[2] = new img("P0002", "End of the Oregon Trail")
>   portlandArray[3] = new img("P0003", "Sacagawea - Washington Park")
>   portlandArray[4] = new img("P0004", "Pittock Mansion, Portland, OR")
>   portlandArray[5] = new img("P0005", "Vista House, Crown Point")
>   }
>
> //===================================================================
>
>
> Bill Ensley
> Bear Printing
>
>
> -----Original Message-----
> From: plug-bounces at lists.pdxlinux.org
> [mailto:plug-bounces at lists.pdxlinux.org] On Behalf Of Richard C. Steffens
> Sent: Wednesday, September 14, 2005 12:07 PM
> To: PLUG List
> Subject: [PLUG] Javascript Array Problem
>
> I'm working on adding a category of images to my photo website. (The problem
> described below is on my machine at home, and not up on the web, yet, so
> I'll just have to describe it.)
>
> I have a set of arrays to hold information about the images in each
> category, and I build them with the following javascript code:
>
> //===================================================================
>   // Function to initialize an array of n entries
>   function MakeArray(n) {
>     this.length = n
>     for (var i = 1; i <= n; i++) {
>       this[i] = 0
>     }
>     return this
>   }
>
>
> //=================================================================
>   // constructor function for array elements
>   function img(code, caption, qty0, qty1, qty2, qty3, qty4) {
>     this.code = code
>     this.caption = caption
>     this.qty0 = 0
>     this.qty1 = 0
>     this.qty2 = 0
>     this.qty3 = 0
>     this.qty4 = 0
>     }
>
> //======================================================================
> function buildPortlandArray() {
>   portlandArray[1] = new img("P0001", "Oregon City Bridge")
>   portlandArray[2] = new img("P0002", "End of the Oregon Trail")
>   portlandArray[3] = new img("P0003", "Sacagawea - Washington Park")
>   portlandArray[4] = new img("P0004", "Pittock Mansion, Portland, OR")
>   portlandArray[5] = new img("P0005", "Vista House, Crown Point")
>   }
>
> //===================================================================
> function initializeSite() {
> // other things happen in this function, but I cut out most of it for // the
> sake of brevity
>
>   var portlandImageCount = 5
>
>   portlandArray = MakeArray(portlandImageCount)
>   buildPortlandArray()
>   }
>
> In the category I'm trying to add with buildPortlandArray, the first two
> entries of the array return as "undefined" but the last three work just
> fine. There are four other arrays on the site that all work as expected.
>
> Has anyone ever seen something like this before, where the first two entries
> in an array don't work, but the last three do?
>
> Thanks for any ideas you may have to offer.
>
> --
> Regards,
>
> Dick Steffens
> http://home.comcast.net/~rsteff/
>
> Note Cards & Photographic Prints available at:
> http://home.comcast.net/~rcsphoto/
> _______________________________________________
> PLUG mailing list
> PLUG at lists.pdxlinux.org
> http://lists.pdxlinux.org/mailman/listinfo/plug
>
> _______________________________________________
> PLUG mailing list
> PLUG at lists.pdxlinux.org
> http://lists.pdxlinux.org/mailman/listinfo/plug
>



More information about the PLUG mailing list