Reputation: 451
I want to compare one value against all values in the other array and extact a match. These are different in length.
Imagine one column of unsorted ID's and one column of ID's and names. I came up with this, but it doesn't work.
private function mergeCollections():void
{
for (var k:int = 0;k <= idArray.length;k++)
{
for(var i:int = 0; i <= namesArray.length;i++){
if(idArray[k].id == namesArray[i].id){
idArray[k].name = namesArray[i].name;
}
}
}
}
This is what I want to resolve:
Array Coll idArray = [{3}{" "},
{1}{" "},
{2}{" "}]
Array Coll namesArray[{1}{Name1},
{2}{Name2},
{3}{Name3},
{4}{Name4}]
Result =
idArray = [{3}{Name3},
{1}{Name1},
{2}{Name2}]
Sorry if the sintaxis is wrong, my arraycollections arrive from database.
The real thing I'm doing here is recieve values from a table full of ids and a table full of those ides, plus the name (they are separate because, normalization dude!) And I want to put them inside a datagrid that will only display the name.
I say possibly unsorted because the second column might reach a point of several deletes and inserts and might end up unsorted. But for this case I have one unsorted column against one sorted column.
My current output is just one name printed, and the rest dismissed completely.
NOTE: Also take in account that the nameArray has always MORE or EQUAL values to the ID table. and the Id table will always have values that match the names table.
Upvotes: 0
Views: 221
Reputation: 325
Other than using <= rather than <, I'm not sure what's wrong with your code, too bad you don't say what the output is. I wrote this, almost identical to what you did:
var arr1:ArrayCollection = new ArrayCollection([{id:1, name:""}, {id:2, name:""}, {id:3, name:""}]);
var arr2:ArrayCollection = new ArrayCollection([{id:1, name:"1"}, {id:3, name:"3"}, {id:2, name:"2"}, {id:4, name:"4"}]);
for (var i:int=0; i<arr1.length; i++)
{
for (var j:int=0; j<arr2.length; j++)
{
if (arr1[i].id==arr2[j].id)
{
arr1[i].name = arr2[j].name;
trace(arr1[i].id,arr1[i].name);
}
}
}
And it seems to do the trick.
Upvotes: 1