Actionscript 3 合并两个ArrayCollection-Flex
我有两个ArrayCollection,我想把它们合并成一个Actionscript 3 合并两个ArrayCollection-Flex,actionscript-3,apache-flex,actionscript,flex4.5,arraycollection,Actionscript 3,Apache Flex,Actionscript,Flex4.5,Arraycollection,我有两个ArrayCollection,我想把它们合并成一个 arr1 = [0] -> month = 07 tot_err = 15 [1] -> month = 08 tot_err = 16 [2] -> month = 09 tot_err = 17 arr2 = [0] -> month = 07 tot_ok = 5 [1] -> month = 08 tot_ok = 6
arr1 =
[0] -> month = 07
tot_err = 15
[1] -> month = 08
tot_err = 16
[2] -> month = 09
tot_err = 17
arr2 =
[0] -> month = 07
tot_ok = 5
[1] -> month = 08
tot_ok = 6
[2] -> month = 09
tot_ok = 7
我想要这个数组
arr3 =
[0] -> month = 07
tot_err = 15
tot_ok = 5
[1] -> month = 08
tot_err = 16
tot_ok = 6
[2] -> month = 09
tot_err = 17
tot_ok = 7
我怎么做
编辑:
我做了这个解决方案:
private function mergeArrays(a:ArrayCollection, b:ArrayCollection):ArrayCollection
{
for (var i:int=0;i<a.length;i++)
for each(var item:Object in b)
{
if( a[i].month == item.month){
a[i].tot_err = item.tot_err;
}
}
return a;
}
私有函数合并数组(a:ArrayCollection,b:ArrayCollection):ArrayCollection
{
对于(var i:int=0;i如果array2(b)有一个项目。array1(a)中没有的月份使用addItem()方法向array1(a)添加新对象;如果array2(b)有一个项目。array1(a)中没有的月份使用addItem()方法向array1(a)添加新对象
在for循环之后,你可以检查“b”是否还有元素,这样你就可以将它们添加到“a”中,别忘了给“b”数组对象一个默认的tot_ok值。另外,如果“a”中的对象在“b”中没有equalvant,你可以使用这个值
private function mergeArrays(a:ArrayCollection, b:ArrayCollection):ArrayCollection
{
var ex:Boolean = true;
for (var i:int=0;i<a.length;i++){
for each(var item:Object in b)
{
if( a[i].month == item.month){
a[i].tot_err = item.tot_err;
ex = true;
}else{
ex = false;
}
}
if(!ex){
// give a default value here.
a[i].tot_err = 0;
}
}
return a;
}
私有函数合并数组(a:ArrayCollection,b:ArrayCollection):ArrayCollection
{
var-ex:Boolean=true;
对于(变量i:int=0;i
在for循环之后,你可以检查“b”是否还有元素,这样你就可以将它们添加到“a”中,别忘了给“b”数组对象一个默认的tot_ok值。另外,如果“a”中的对象在“b”中没有equalvant,你可以使用这个值
private function mergeArrays(a:ArrayCollection, b:ArrayCollection):ArrayCollection
{
var ex:Boolean = true;
for (var i:int=0;i<a.length;i++){
for each(var item:Object in b)
{
if( a[i].month == item.month){
a[i].tot_err = item.tot_err;
ex = true;
}else{
ex = false;
}
}
if(!ex){
// give a default value here.
a[i].tot_err = 0;
}
}
return a;
}
私有函数合并数组(a:ArrayCollection,b:ArrayCollection):ArrayCollection
{
var-ex:Boolean=true;
对于(var i:int=0;i私有函数mergeArrays(a:ArrayCollection,b:ArrayCollection):ArrayCollection
{
var结果:ArrayCollection=new ArrayCollection();
变量月份:Dictionary=newdictionary();
对于(变量i:int=0;i
私有函数合并数组(a:ArrayCollection,b:ArrayCollection):ArrayCollection
{
var结果:ArrayCollection=new ArrayCollection();
变量月份:Dictionary=newdictionary();
对于(变量i:int=0;i
私有函数合并ArrayCollection(a:ArrayCollection,b:ArrayCollection):ArrayCollection{
var c:ArrayCollection=new ArrayCollection(b.toArray());//克隆b以便不修改b
//此循环处理a和b共有的所有对象
对于每个(变量o:a中的对象){
对于(var i:int=0;i私有函数mergeArrayCollection(a:ArrayCollection,b:ArrayCollection):ArrayCollection{
var c:ArrayCollection=new ArrayCollection(b.toArray());//克隆b以便不修改b
//此循环处理a和b共有的所有对象
对于每个(变量o:a中的对象){
对于(var i:int=0;i问题,您的解决方案不清楚。啊,您修复了我的问题,这现在有意义了。我将从[i]中检查它。tot_err=item.tot_err;您正在将item.tot_err分配给[i].tot_err。它不是合并。示例和您的解决方案是不同的…@M.S.NAyak实际上示例和解决方案是相同的…@Marcx在您的示例中arr2包含tot_ok。在解决方案中什么是item.tot_err?我认为它应该是[i].tot_ok=item.tot_ok;问题和您的解决方案不清楚。啊,您修复了问题,这现在有了意义。我将检查它的a[i]。tot_err=item.tot_err;您正在将item.tot_err分配给a[i].tot_err。它不是合并。示例和您的解决方案是不同的…@M.S.NAyak实际上示例和解决方案是相同的…@Marcx在您的示例中arr2包含tot_ok。在解决方案中什么是item.tot_err?我认为它应该是[i].tot_ok=item.tot_ok;如果第一次不返回true,它将把所有项目放在b到a中。如果第一次不返回true,它将把所有项目放在b到a中。
private function mergeArrays(a:ArrayCollection, b:ArrayCollection):ArrayCollection
{
var result:ArrayCollection = new ArrayCollection();
var months:Dictionary = new Dictionary();
for (var i:int = 0; i < a.length; i++)
{
var mergedItem:Object = new Object();
mergedItem.month = a[i].month;
mergedItem.tot_ok = a[i].tot_ok;
mergedItem.tot_err = null;
for (var j:int = 0; j < b.length; j++)
{
if(a[i].month == b[j].month)
{
mergedItem.tot_err = b[j].tot_err;
}
}
month[mergedItem.month] = true;
result.addItem(mergedItem);
}
// so far we have handled all occurrences between a and b,
// now we need to handle the items from b that are left
for each (var bItem:Object in b)
{
mergedItem = new Object();
mergedItem.month = bItem.month;
mergedItem.tot_err = bItem.tot_err;
mergedItem.tot_ok = null;
if (months[mergedItem.month] == null)
{
month[mergedItem.month] = true;
result.addItem(mergedItem);
}
}
return result;
}
private function mergeArrayCollections(a:ArrayCollection, b:ArrayCollection):ArrayCollection {
var c:ArrayCollection=new ArrayCollection(b.toArray()); //clone b so as not to modify b
//This loop handles all objects common to a and b
for each(var o:Object in a) {
for (var i:int=0; i<c.length; i++) {
var p:Object=c.getItemAt(i);
if(o.month==p.month) {
//if the month is the same then add the property to a
o.tot_ok=p.tot_ok;
c.removeItemAt(i);
break;
}
}
}
//This loop adds the leftover items from c to a
for each(var q:Object in c) {
q.tot_err=-1; //add this so that all objects in a are uniform
a.addItem(q);
}
return a; //Unnecessary return, a will be modified by reference
}