A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: working with arrays

  1. #1
    Junior Member
    Join Date
    Mar 2004
    Posts
    5

    working with arrays

    is there a quick script to clear an array?
    Something like

    myArray.clearArray();

    would be great.

    any help would be great

    Matt

  2. #2
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Hi,

    you could just create a new array with the same name,

    code:

    var myArray = [];


  3. #3
    2HT
    Join Date
    Jul 2000
    Posts
    96

    Best way to delete items

    The best way to go about this is usually to always set the value of the variable/array/etc. to null as explained by catbert303. Good advice from him :-)

  4. #4
    Junior Member
    Join Date
    Mar 2004
    Posts
    5
    ok, good advice.
    I got it settled with a button that does this:

    clear_btn.onRelease = function() {
    for (i=0; i<_root.drinkArray.length; i++){
    _root.drinkArray [i] = "";
    }
    drink_list.setDataProvider(drinkArray);
    }

    second question:

    would

    myArray[i] = [];

    remove an element from my array?
    would this effect the length of the array?

    matt

  5. #5
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Hi,

    myArray[i] = [];

    wouldn't change the size of myArray, it would just mean at position i the element was an empty array. to remove the element at position i (and reduce the length of the array by 1) you could use the splice method,

    myArray.splice(i, 1);

  6. #6
    Junior Member
    Join Date
    Mar 2004
    Posts
    5
    that's the thing.

    so,

    myArray.splice(0);

    would clear the whole thing and shorten the length of the array to zero?

    matt

  7. #7
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    yep

  8. #8
    Junior Member
    Join Date
    Mar 2004
    Posts
    5
    cool thanks!

    see the working thing

    matt

  9. #9
    Member
    Join Date
    May 2004
    Location
    Raleigh NC
    Posts
    34
    i get some weird thing ahppenin whenever i use splice. the object i removed from the array, but it leaves an empty spot. after a while my array looks like [,,,,,,,,,]. why is it placing an empty spot?
    so there!

  10. #10
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    what does your splice code look like? splice can be used to remove elements from an array and also insert things in their place.

  11. #11
    Member
    Join Date
    May 2004
    Location
    Raleigh NC
    Posts
    34
    i use a getIndex function to find its index and set it to x.
    so its

    var x = getIndex(this);
    eArray.splice(x, 1);
    so there!

  12. #12
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    what happens if you try testing a movie using,

    trace("original eArray...");
    trace(eArray.join("\n"));
    var x = getIndex(this);
    trace("x = " + x);
    eArray.splice(x, 1);
    trace("new eArray...");
    trace(eArray.join("\n"));

  13. #13
    Member
    Join Date
    May 2004
    Location
    Raleigh NC
    Posts
    34
    oops, i defined getIndex as a prototype and was using it wrong.
    so that mystery is solved
    thanks catbert
    so there!

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center