mheavers
mheavers

Reputation: 30158

flash as3 best way to create multidimensional array

I have an array of movie clips (representing band members) which have various properties, among them them a property which tells where the band member went to after they left their current band. For those who formed a new group, I want to create an array. Within that array, I want to group all the ones that left for the same group into secondary arrays. So, if you had five band members and 2 of them left for group X and 3 left for group Y. What's the best way to do this? This is, roughly, my code:

var newGroupArr:Array = new Array() //this will hold all members leaving for a new group

for (k=0;k<memberClips.length;k++){
    if (memberClips[k].outcome == "new"){
        //for all groups where memberClips[k].group is the same, create a new array within newGroupArr for those similar members.
    }
}

Alternatively I suppose if I could do without a multidimensional array and just loop through all the members and say - for those members whose group is the same, perform this function, with the name of that same group passed as the parameter for the function. I guess the trouble I'm having is identifying who's the same.

This illustration shows the problem I'm having - if John Wetton is clicked, a line should be drawn only for him. But instead, a line is drawn for both he, david cross, and bill bruford, because they are all leaving for a new group. But david cross and bill bruford are actually going to a different group than john, so I need to make sure they are stored as members leaving for a new group, but I also need them grouped by the new band they are leaving for.

Upvotes: 0

Views: 2092

Answers (2)

duggulous
duggulous

Reputation: 2727

Unless there's a reason why you need to use an array, I'd use a Dictionary, like so

var bands:Dicionary = new Dictionary();

for (k=0;k<memberClips.length;k++){
    if(memberClips[k].outcome=="new"){
        var newGroup:String = memberClips[k].group;
        if(!bands[newGroup]){
           bands[newGroup] = new Array();
        }
        bands[newGroup].push(k);
    }
}

Now each array in bands will contain the members that left their previous band

Upvotes: 1

Related Questions