Thursday, November 13, 2008

How to copy an ArrayCollection


There are many ways to copy an arrayCOllection to another one.Here is a couple of them

Initialize the new array with the older arrayCollection's source.

Example :var a : ArrayCollection = new ArrayCollection(array);   var b : ArrayCollection = new ArrayCollection(a.source);

Use mx.utls.ObjectUtil.Copy

Example :var arrayCollection1 : ArrayCollection = ObjectUtil.copy(arrayCollection2)

Hope it helps!

Bookmark and Share


Octave said...

yes it doses help. thx a lot

Anonymous said...

In the instance when you have to build your own ArrayCollection from multiple data sources the following shortened code (I use a loop to create the parent and an inside loop to create the child) to add the child to the parent. To copy one ArrayCollection into another, try this:

var childArray:ArrayCollection([{label:"ALL",data:null},

var parentArray:ArrayCollection
([{label:"ALL",data:null,, children:new ArrayCollection(_ChildAC.toArray())}}]);

Using the toArray clones the array instead of creating a reference to it. This allows you to reuse the variable inside the loop instead of trying to create multiple new arrays.

Anonymous said...

Thank you. This is very helpful

Narasimha said...

You have shown one of the best way to take a copying of an arraycollection :)

Lombardi said...

Thanks men.
mx.utls.ObjectUtil.Copy didn't work for me, don't know why, but the first one works very well.
It was very helpful.

Another way to copy one object "a" to a "b":
for each (var item:Object in a){

Anonymous said...

Thanxs .. Kip Sharing ..!!

kishorekumaru said...

Hi machi,
Thanks It really works :) Good to see your blog too.

emwork said...

Many Thanks! for cool trips

YAMUNA said...

It's very Helpful.. Before this, i tried:

var arr1: ArrayCollection = new ArrayCollection();
var arr2:ArrayCollection = new ArrayCollection();

then i add some objects in arr1 as arr1.addItem(obj); and its a dynamic list.. Its varies user type a charcter..

And arr2 have server original array collection. At some particular point, i need original array in datagrid..
So i moved arr2 to arr1 as
arr2 = arr1;
But i get only arr1 objects not original objects..

Pls tell the diff b/w arr1.source = arr2.souce and arr1 = arr2

Anonymous said...

This doesn't actually copy the ArrayCollection, only the objects. ArrayCollections have filter/sorting backing, which, if using either one of these methods, is not provided.