0

I have an 2D array as:

Asia India 100 200
Asia China 200 300

I need to add an attribute named "state" with value false to this above array (internally) as an additional column to the above array where my output is :

Asia India 100 200 state:false
Asia China 200 300 state:false

so that I can give the additional column's attribute name "state" to item renderer. How to get the same.?Is it possible?

2
  • I am a bit confused. Based on the info you have provided, you have an array of strings. Is that correct? Or do you have an array of objects? It does not make sense to me to mix strings with object properties--although I do believe it is technically possible. Commented Jul 29, 2013 at 11:45
  • Actually in my previous posting regarding AdvancedDataGrid,using checkbox control as GroupItemRenderer you had provided answer to my question as to use data property and pass the property to set selected unselected states from that.Am in attempt to do the same for which I need to populate a property named 'state' in last column of my original array so that i can set that property of a particular clicked row to true on selection,false on deselection respy. Commented Jul 30, 2013 at 6:01

1 Answer 1

1

For lines:

var originalArray:Array = //your original array

var newArray:Array = new Array();

 for each ( var o:Object in originalArray){
    o.state = "false";
    newArray.push(o);
 }

 originalArray = newArray;

 trace(originalArray[0]) // Asia India 100 200
 trace(originalArray.state) //false

using Array.map() should work too.

For invidual objects:

        protected function addStates():void
        {

            var originalArray:Array = //your original array
            var newArray:Array = new Array();               

            for each( var o:Object in originalArray){                       
                var tempArray:Array = new Array();
                for each(var element:* in o){
                    var newObject:Object = new Object();
                    newObject.element = element;
                    newObject.state = "false";
                    tempArray.push(newObject);

                }
                newArray.push(tempArray);
            }

            originalArray = newArray;       
            trace(originalArray[0][1].element); // India
            trace(originalArray[0][1].state); // false

        }

If someone has a better/faster way I am interested to hear it.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm having a hard time with this answer. First, you set originalArray = new Array(); immediately followed by a few traces of the originalArray. Why would those traces return anything? Second, the original question states he has a 2 dimensional array; but I'm not clear if this code will deal with more than 1 dimension.
Corrected the typo, sorry, tested with 2D array and it seemed to work.
@Pete TNT Is this possible to modify the above array to be normal 2D array so that I get the desired output where I can get the 'state' property directly from the original array something like "originalArray[row][column].state"
@PeteTNT Yes this is somewhat nearing to the expected result.I integrated your code with mine but it affects the dataprovider so I need the last column of the array alone to be with the property state rather than each element of array having that 'state' attribute.How can I get that format?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.