PDA

View Full Version : Comparing 2 or more Arrays. How?


suchislife801
11-04-2009, 11:41 PM
Hello experts,

I've been working on a small project and have been stuck on this for a few days. Let me show you some of the code:

1. I declare 3 Bindable Arrays.


private var arrTeam:Array;

[Bindable]
private var arrStats1:Array;

[Bindable]
private var arrStats2:Array;

[Bindable]
private var arrStatsFinal:Array;


2. I create the init() function that initializes these arrays populating some of them.


private function init():void
{

arrTeam = new Array();
arrTeam.push({col0:'vini', col1:'martins'});
arrTeam.push({col0:'trent', col1:'easton'});
arrTeam.push({col0:'jeniffer', col1:'valandingham'});

arrStats1 = new Array();
arrStats1.push({col0:'vini', col1:'martins', col2:100});
arrStats1.push({col0:'jeniffer', col1:'valandingham', col2:300});

arrStats2 = new Array();
arrStats2.push({col0:'trent', col1:'easton', col2:150});
arrStats2.push({col0:'jeniffer', col1:'valandingham', col2:250});

arrFinal = new Array();

// For each employee found in arrTeam...
arrTeam.forEach(traceEmployee);

}


3. This is the function that I think I should run for each time we iterate through arrTeam's indexes... Also where I need help because it is incomplete.


private function traceEmployee(element:*, index:Number, arr:Array):void
{

// Each time this function gets called..
for (var i:int=0; i < arrStats1.length; i++)
{
// If we find a match in arrStats1 then..
if (arrStats1[i].col0 == element.col0)
{
// Populate arrFinal with it.
arrFinal.push({col0:element.col0, col1:element.col1, col2:arrStats1[i].col2});
}

}


4. As you can see, the function above only checks arrTeam against arrStats1. I need to find a way to...

a. If name from arrTeam is found inside arrStats1 then check arrStats2.
b. If name from arrTeam is found inside arrStats2 then add both to arrFinal.
c. If name from arrTeam is NOT found inside arrStats1 then check arrStats2.
d. If name from arrTeam is NOT found inside arrStats2 then add only the first and last name.
e. If name from arrTeam is found inside arrStats1 then check arrStats2.
f. If name from arrTeam is NOT found inside arrStats2 then add first, last name and Stats1 to arrFinal.
g. If name from arrTeam is NOT found inside arrStats1 then check arrStats2.
h. If name from arrTeam is found inside arrStats2 then then add first, last name and Stats2 to arrFinal.

Greg SS
11-04-2009, 11:53 PM
Why not use a simple for..loop?

suchislife801
11-04-2009, 11:59 PM
Can you show me how? It's not so simple for me.

Greg SS
11-05-2009, 12:18 AM
a. If name from arrTeam is found inside arrStats1 then check arrStats2.
b. If name from arrTeam is found inside arrStats2 then add both to arrFinal.
c. If name from arrTeam is NOT found inside arrStats1 then check arrStats2.
d. If name from arrTeam is NOT found inside arrStats2 then add only the first and last name.
e. If name from arrTeam is found inside arrStats1 then check arrStats2.
f. If name from arrTeam is NOT found inside arrStats2 then add first, last name and Stats1 to arrFinal.
g. If name from arrTeam is NOT found inside arrStats1 then check arrStats2.
h. If name from arrTeam is found inside arrStats2 then then add first, last name and Stats2 to arrFinal.

well, thats a lot of requirement...
but I guess you can do it like this?


private function init():void {
arrTeam = new Array();
arrTeam.push({col0:'vini', col1:'martins'});
arrTeam.push({col0:'trent', col1:'easton'});
arrTeam.push({col0:'jeniffer', col1:'valandingham'});

arrStats1 = new Array();
arrStats1.push({col0:'vini', col1:'martins', col2:100});
arrStats1.push({col0:'jeniffer', col1:'valandingham', col2:300});

arrStats2 = new Array();
arrStats2.push({col0:'trent', col1:'easton', col2:150});
arrStats2.push({col0:'jeniffer', col1:'valandingham', col2:250});

arrStatsFinal = new Array();

for(var i:int=0; i<arrTeam.length; i++){
var obj:Object = new Object();
arrStatsFinal.push(obj);
obj.col0 = arrTeam[i].col0;
obj.col1 = arrTeam[i].col1;
for(var j:int=0; j<arrStats1.length; j++){
if(arrStats1[j].col0 == arrTeam[i].col0 && arrStats1[j].col1 == arrTeam[i].col2) obj.col2 = arrStats1[j].col2 else col2 = null;
}
for(j=0; j<arrStats2.length; j++){
if(arrStats2[j].col0 == arrTeam[i].col0 && arrStats2[j].col1 == arrTeam[i].col2) obj.col3 = arrStats2[j].col2 else col3 = null;
}
}

}

suchislife801
11-05-2009, 01:41 AM
[Bindable]
private var arrTeam:Array;

[Bindable]
private var arrStats1:Array;

[Bindable]
private var arrStats2:Array;

[Bindable]
private var arrStatsFinal:Array;

private function init():void {
arrTeam = new Array();
arrTeam.push({col0:'vini', col1:'martins'});
arrTeam.push({col0:'trent', col1:'easton'});
arrTeam.push({col0:'jeniffer', col1:'valandingham'});

arrStats1 = new Array();
arrStats1.push({col0:'vini', col1:'martins', col2:100});
arrStats1.push({col0:'jeniffer', col1:'valandingham', col2:300});

arrStats2 = new Array();
arrStats2.push({col0:'trent', col1:'easton', col2:150});
arrStats2.push({col0:'jeniffer', col1:'valandingham', col2:250});

arrStatsFinal = new Array();

// For each name on the team
for(var i:int=0; i<arrTeam.length; i++)
{
// Create and empty object
var obj:Object = new Object();
// Added it to the final array.
arrStatsFinal.push(obj);
// Object first column = first name
obj.col0 = arrTeam[i].col0;
// Object second column = last name
obj.col1 = arrTeam[i].col1;

for(var j:int=0; j<arrStats1.length; j++)
{
// stats1 first, last = team first, last
if(arrStats1[j].col0 == arrTeam[i].col0 && arrStats1[j].col1 == arrTeam[i].col1)
{
// then inject stat1 into 3rd column of the object we are creating.
obj.col2 = arrStats1[j].col2;
break;
} else
obj.col2 = 'n/a';
}

for(var k:int=0; k<arrStats2.length; k++)
{
// stats2 first, last = team first, last
if(arrStats2[k].col0 == arrTeam[i].col0 && arrStats2[k].col1 == arrTeam[i].col1)
{
// then inject stat2 into 3rd column of the object we are creating.
obj.col3 = arrStats2[k].col2;
break;
} else
obj.col3 = 'n/a';
}

}

}