...

View Full Version : Syntax to populate an array in JavaScript



trekie
04-20-2005, 01:34 PM
I am not sure on the correct syntax for declaring an array that has a total of 23 persons. The names are not important.
I thought the syntax was

Person = new Array (1 to 23)

but this does not seem to work.
Thanks in advance.
Trekie

jbot
04-20-2005, 01:37 PM
Could anyone help with a problem i have populating an array.

Google (http://www.google.co.uk/search?client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial_s&hl=en&q=javascript+arrays&meta=&btnG=Google+Search) would, if you weren't so lazy to actually search the thing.

Roelf
04-20-2005, 01:46 PM
i never give the initial size of an array when i create one. Javascript dynamically resizes the array if you put an element in a specific position, in your case:


var Person = new Array();
Person[Person.length] = "thename";
Person[Person.length] = "anothername";

never hardcode the indices when filling an array, maintenance can be a pain in the behind

trekie
04-20-2005, 01:56 PM
Roelf
Thank you for your help.
I will try it out.

Kor
04-21-2005, 09:03 AM
var Person = new Array();
Person[Person.length] = "thename";
Person[Person.length] = "anothername";


I seriousely doubt that Array is a valid one. Probably you would like to write

var Person = new Array();
Person[0] = 'thename';
Person[1] = 'anothername';

this can be also written in different modes
var Person = new Array('thename,'anothername');
or
var Person = ['thename,'anothername'];
or
var Person = [];
Person[0] = 'thename';
Person[1] = 'anothername';

glenngv
04-21-2005, 09:50 AM
I seriousely doubt that Array is a valid one. Probably you would like to write

That is perfectly valid and is actually the same with hardcoding it. Although hardcoding the index is more efficient, but the performance difference may be minimal.

Kor
04-21-2005, 09:53 AM
if

var Person = new Array();
Person[Person.length] = "thename";
Person[Person.length] = "anothername";

is a valid code... how am I suppose to get one of the array's values? For me, the code above is similar with

var Person = new Array();
Person[2] = "thename";
Person[2] = "anothername";

glenngv
04-21-2005, 10:06 AM
For me, the code above is similar with

var Person = new Array();
Person[2] = "thename";
Person[2] = "anothername";
Wrong.

var Person = new Array(); //Person.length = 0
Person[Person.length] = "thename"; //Person.length = 0, becomes 1 after the assignment
Person[Person.length] = "anothername"; //Person.length = 1, becomes 2 after the assignment

So that is essentially the same with:

var Person = new Array();
Person[0] = 'thename';
Person[1] = 'anothername';

If you still don't believe, try this:

var Person = new Array();
Person[Person.length] = 'thename';
Person[Person.length] = 'anothername';
for (var i=0;i<Person.length;i++){
alert("Person["+i+"]="+Person[i]);
}

Kor
04-21-2005, 10:12 AM
Yes, Sir, I reckon you are right. It is one of those simpliest things one never think about... ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum