Wouldn't doing the splice there change the indexes of everything else
in the array and mess up the loop?

For instance:

[1, 2, A, A, 5, A, 7]

say you're deleting the As.

The first time you do a splice you remove the first one, and get:

[1, 2, A, 5, A, 7]

The index of everything after the first A has now changed.  The second
one is now at index 3, but you already handled index 3 in the for
loop.  Is the for-in loop smart enough to run index 3 again, since the
value there is new?

  -Andy

On 12/21/06, T. Michael Keesey <[EMAIL PROTECTED]> wrote:
Quite simple:

for (var key:String in myArray1) {
    if (myArray1[key][1] < 0) {
        myArray1.splice(Number(key), 1);
    }
}

BTW, "myArray1" is not a great name for a variable ... why not
"scoreRecords" or "scores" or something? Also, storing different types
of data in an array is a recipe for confusions. The element arrays
would be better as objects, e.g.: {name: "A", score: -1}.Then the code
would be more readable:

for (var key:String in scoreRecords) {
    var record:Object = scoreRecords[key];
    if (record.score < 0) {
        scoreRecords.splice(Number(key), 1);
    }
}

Or the whole thing could just be an associative array (i.e., hash object):

var scores:Object = {A: -1, B: -1, C: 0};
// ...
for (var key:String in scoreRecords) {
    if (scoreRecords[key] < 0) {
        delete scoreRecords[key];
    }
}

On 12/21/06, Mike Cobb <[EMAIL PROTECTED]> wrote:
> -
>
> Hi everyone,
>
> I'm having a braindead moment today, which I was hoping someone could
> help me with.
>
> I have the following array...
>
> //Create array with 5 elements
> var myArray1:Array = new Array();
> myArray1.push( new Array("A:", -1) );
> myArray1.push( new Array("B:", -1) );
> myArray1.push( new Array("C:",  0) );
> myArray1.push( new Array("D:",  0) );
> myArray1.push( new Array("E:", -1) );
> myArray1.push( new Array("F:",  1) );
> myArray1.push( new Array("G:",  0) );
> myArray1.push( new Array("H:", -1) );
>                         //name, score
>
> ...and I'm trying to remove all the elements in myArray1 with a score of
> less than 0 without sorting/reordering the array at all.
>
> I was trying to use a 'for loop', but obviously as the elements are
> removed, the length of the array changes & causes the wrong elements to
> be deleted.
>
> Can anyone help?
>
> Thanks,
> <Mike>
>
> --
> -------------------------------------
> Mike Cobb
> Creative Director
> HMC Interactive
> -------------------------------------
> Tel: + 44 (0)845 20 11 462
> Mob: + 44 (0)785 52 54 743
> Web: http://www.hmcinteractive.co.uk
> -------------------------------------
> Grosvenor House, Belgrave Lane,
> Plymouth, PL4 7DA, UK.
> -------------------------------------
>
> I've got a new e-mail address: [EMAIL PROTECTED]
> Please update your address book. Thanks.
>
> _______________________________________________
> Flashcoders@chattyfig.figleaf.com
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>


--
T. Michael Keesey
Director of Technology
Exopolis, Inc.
2894 Rowena Avenue Ste. B
Los Angeles, California 90039
--
The Dinosauricon: http://dino.lm.com
Parry & Carney: http://parryandcarney.com
ISPN Forum: http://www.phylonames.org/forum/
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to