Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. JavaScript
  4. Adding Var Object to An Array Dynamically

Adding Var Object to An Array Dynamically

Scheduled Pinned Locked Moved JavaScript
data-structures
4 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    Hi all, I know that normally one can add string(s) to an array upon declaring the array by placing the string or strings inside the parenthesis, surrounded by double quotes and separating them with a comma. However, I would like to dynamically add them by doing the following but I cannot get it to work:

    var stringArray[] = new Array(3);

    //Declaring string objects
    vary string1 = "First";
    vary string2 = "Second";
    vary string3 = "Third";

    //Then in a for loop add each string to the array
    for(x=0; x
    Please explain why the code above does not work, in detail thanks.

    G H 2 Replies Last reply
    0
    • A ASPnoob

      Hi all, I know that normally one can add string(s) to an array upon declaring the array by placing the string or strings inside the parenthesis, surrounded by double quotes and separating them with a comma. However, I would like to dynamically add them by doing the following but I cannot get it to work:

      var stringArray[] = new Array(3);

      //Declaring string objects
      vary string1 = "First";
      vary string2 = "Second";
      vary string3 = "Third";

      //Then in a for loop add each string to the array
      for(x=0; x
      Please explain why the code above does not work, in detail thanks.

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      Your first line contains a syntax error, so none of the code will execute. You don't need the square brackets when you declare an array in Javascript (I think you do in Java, but it's been years since I did any).

      var stringArray = new Array(3);

      Apart from that, you have "vary" instead of "var" for your three strings, and you are terminating the loop one item too early. To use the actual string variables you declared, you have to eval the string you create using the loop index like this:

      = eval("string" + (x + 1));

      The "x + 1" is in parentheses so that the number x + 1 is appended to the string instead of x and 1 being appended separately.

      A 1 Reply Last reply
      0
      • G Graham Breach

        Your first line contains a syntax error, so none of the code will execute. You don't need the square brackets when you declare an array in Javascript (I think you do in Java, but it's been years since I did any).

        var stringArray = new Array(3);

        Apart from that, you have "vary" instead of "var" for your three strings, and you are terminating the loop one item too early. To use the actual string variables you declared, you have to eval the string you create using the loop index like this:

        = eval("string" + (x + 1));

        The "x + 1" is in parentheses so that the number x + 1 is appended to the string instead of x and 1 being appended separately.

        A Offline
        A Offline
        ASPnoob
        wrote on last edited by
        #3

        Hi thanks for replying, I did not realize that Microsft Word auto-correct changed the var keywords to vary. I was able to get the code to work with your help.

        1 Reply Last reply
        0
        • A ASPnoob

          Hi all, I know that normally one can add string(s) to an array upon declaring the array by placing the string or strings inside the parenthesis, surrounded by double quotes and separating them with a comma. However, I would like to dynamically add them by doing the following but I cannot get it to work:

          var stringArray[] = new Array(3);

          //Declaring string objects
          vary string1 = "First";
          vary string2 = "Second";
          vary string3 = "Third";

          //Then in a for loop add each string to the array
          for(x=0; x
          Please explain why the code above does not work, in detail thanks.

          H Offline
          H Offline
          Hassan Oumar Mahamat
          wrote on last edited by
          #4

          Your code was not working because of the [] in declaration of your variable

          stringArray

          Try this one out

          var stringArray = new Array(3);
          //Then in a for loop add each string to the array
          for(x=0; x<stringArray.length-1; x ++)
          {
          //Add string
          stringArray[x]= "string" + (x+1);
          }

          If you add "string"+x+1 the output will be string11 string21 string31 I'm sure that's not what you want.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups