How do I reset an array!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bdbeames
    New Member
    • Jun 2007
    • 27

    How do I reset an array!

    I have and array that I fill with elements in a function.

    I call it later and want to reset it so that it is empty so I can fill it with different elements and a different number of elements. How do I reset it so that there is nothing in it? I would do it before the If statements, but I'm not sure how to reset it. Could someone help?

    [code=javascript]

    var imageArray;
    var image_dir;
    var imageNum =0;

    function rotate(pictype)
    var i = 0;

    if (picType =="weather_pic" ) {
    image_dir = "img/w/";
    type = "id_w";
    imageNum =0;
    imageArray = new Array();

    for (i=1;i<=26;i++) {
    imageArray[imageNum++] = new imageItem(image _dir + i + ".jpg");
    }
    }
    if (picType =="optical_pic" ) {
    image_dir = "img/o/";
    type = "id_o";
    imageNum =0;
    imageArray = new Array();

    for (i=1;i<=56;i++) {
    imageArray[imageNum++] = new imageItem(image _dir + i + ".jpg");
    }
    }

    totalImages = imageArray.leng th;
    switchImage(typ e);
    [/code]
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, BD.

    Setting imageArray = new Array() should do the job. If you want to make extra sure, you can delete it, too:

    [code=javascript]
    delete imageArray;
    imageArray = {};
    [/code]

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      moved over form articles to forum section ...

      to reset an array there are different ways:

      [CODE=javascript]
      // 1. simply create a new instance

      // call the constructor ... hint:
      // in case you don't give args to a constructor
      // leave the backets because js evals them and
      // in our case it needn't
      arr = new Array;

      // do it with literals - best way ;)
      arr = [];

      // 2. set the length to 0 = ugly hack ;)
      arr.length = 0;[/CODE]

      kind regards

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by gits
        moved over form articles to forum section ...

        to reset an array there are different ways:

        [CODE=javascript]
        // 1. simply create a new instance

        // call the constructor ... hint:
        // in case you don't give args to a constructor
        // leave the backets because js evals them and
        // in our case it needn't
        arr = new Array;

        // do it with literals - best way ;)
        arr = [];

        // 2. set the length to 0 = ugly hack ;)
        arr.length = 0;[/CODE]

        kind regards

        Why this line is best?
        arr = [];
        Any reason GITS.
        Please help.

        Kind regards,
        Dmjpro.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          Originally posted by dmjpro
          Why this line is best?
          arr = [];
          Any reason GITS.
          Please help.

          Kind regards,
          Dmjpro.
          hi ...

          it is the shortest way to create an array ... and during parsing and evaluating it gives the best performance with it because js don't need to read the new and array keywords and interpret them ... simply read and interpret 2 characters ...

          kind regards

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            It also helps dispel the illusion that many people have that there is such a thing as an associative Array in JavaScript....

            Comment

            Working...