Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Syntax to populate an array in JavaScript

    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
    Last edited by trekie; 04-20-2005 at 02:45 PM. Reason: To make understanding the problem clearer to understand

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by trekie
    Could anyone help with a problem i have populating an array.
    Google would, if you weren't so lazy to actually search the thing.
    *keep it simple (TM)

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    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:
    Code:
    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
    I am the luckiest man in the world

  • #4
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you for the tip

    Roelf
    Thank you for your help.
    I will try it out.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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';
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Kor
    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.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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";
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Kor
    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:
    Code:
    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]);
    }
    Last edited by glenngv; 04-21-2005 at 11:10 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Yes, Sir, I reckon you are right. It is one of those simpliest things one never think about...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •