A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Arrays get the next available index

  1. #1
    Dance Monkey Dance! Doush.'s Avatar
    Join Date
    Jun 2004
    Posts
    254

    Arrays get the next available index

    I'm just fooling around making an inventory system and I was wondering if there is a way to work out the next available index in an array without pushing the item into the array.

    So for instance

    var array:Array = new Array();

    array[0] = new Object();
    array[1] = new Object();
    array[3] = new Object();

    Nothing is set in array index 2 so that is the id I want to get or if all indexes are filled eg

    array[0] = new Object();
    array[1] = new Object();
    array[2] = new Object();

    I will get array index 3?

    Is this possible?
    "I layed down in my bed last night looked up at the stars, and thought to myself... Where the F*#K is my roof"

  2. #2
    Senior Member
    Join Date
    May 2010
    Posts
    178
    Is this you are talking about?


    Actionscript Code:
    var array:Array = new Array();

    array[0] = new Object();
    array[1] = new Object();
    array[3] = new Object();

    //Nothing is set in array index 2 so that is the id I want to get or if all indexes are filled eg

    //array[0] = new Object();
    //array[1] = new Object();
    //array[2] = new Object();

    for(var i=0;i<array.length;i++){
        if(array[i]==undefined){
            trace(i);
            array[i]=new Object();
        }
    }
    trace(array);


    poltuda

  3. #3
    Dance Monkey Dance! Doush.'s Avatar
    Join Date
    Jun 2004
    Posts
    254
    That's perfect. I didn't realise you could check for undefined on empty array values.

    Because this is an array that holds inventory instead of splicing items from the array to remove them, should I set it to undefined instead so the array length doesn't change?

    Thanks for the reply
    "I layed down in my bed last night looked up at the stars, and thought to myself... Where the F*#K is my roof"

  4. #4
    Senior Member
    Join Date
    May 2010
    Posts
    178
    Actionscript Code:
    var array:Array = new Array();

    array[0] = new Object();
    array[1] = new Object();
    array[3] = new Object();

    //Nothing is set in array index 2 so that is the id I want to get or if all indexes are filled eg

    //array[0] = new Object();
    //array[1] = new Object();
    //array[2] = new Object();

    for(var i=0;i<array.length;i++){
        if(array[i]==undefined){
            trace(i);
            array[i]=new Object();
        }
    }
    trace(array);
    trace(array.length);

    array[3]=undefined;
    trace(array);
    trace(array.length);


    poltuda

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