3 Replies Latest reply on Aug 17, 2010 2:57 PM by laxmidi

    Is It Possible to Use a Function When Creating an Array?

    laxmidi Level 1

      Hi,

       

      Is it possible to use a function when building an array?

       

      I'd like to iterate my data returned from the db and call a function. The part where I call the function "myFunction[i]" doesn't seem to work. How do I solve this problem? I need to put the results of the function call into the array.

       

      var i:uint;

      for (i=0; i<myDataArray.length; i++)

      {

      dgArray = [{Name: myDataArray[i].name, Type: myFunction[i]}];

      }

       

       

      Thank you!
      -Laxmidi
        • 1. Re: Is It Possible to Use a Function When Creating an Array?
          BhaskerChari Level 4

          Hi laxmi,

           

          Yes ofcourse it is very much possible to use a Function when creating an array...In your code you have made some mistakes..

           

          You have used square bracket for calling the function myFunction[i]....where you are supposed to use parantheses.....myFunction(i)...and other this is you need to push the object into the dgArray but yous hould not not assign it...

           

          dgArray = [{Name: myDataArray[i].name, Type: myFunction[i]}]; // this will result in only the last iterated Array to be in dgArray..

           

          So you should write  dgArray.push({Name: myDataArray[i].name, Type: myFunction(i)});

           

          Check out the code below....

           

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
          <mx:Script>
            <![CDATA[
             private var dgArray:Array = new Array();
             private var myDataArray:Array = [{data:"1", name:"Ramu"},{data:"2", name:"Sharath"},{data:"3", name:"Bharath"}]
           
             private function init():void
             {
              for (var i:int=0; i<myDataArray.length; i++)
              {
               dgArray.push({Name: myDataArray[i].name, Type: myFunction(i)});
              }
             }
            
             private function myFunction(i:int):String
             {
              return "Type" + i;
             }
            ]]>
          </mx:Script>
          </mx:Application>

           

          If this post answers your question or helps, please kindly mark it as such.


          Thanks,

          Bhasker Chari

          • 2. Re: Is It Possible to Use a Function When Creating an Array?
            Subeesh Arakkan Level 4

            .

             

            Message was removed by: Subeesh Arakkan

            • 3. Re: Is It Possible to Use a Function When Creating an Array?
              laxmidi Level 1

              Hi Bhasker Chari,

               

              Thank you very much for your detailed reply! I really appreciate your help.

               

              -Laxmidi