12 Replies Latest reply on Feb 5, 2009 12:11 PM by AlHolden

    Contructing an Object with dynamic property names?

    AlHolden Level 1
      So the live docs for "Object" construction reads like this:
      var obj:Object = {a:"foo", b:"bar"}

      But what if I need to build an Object using run-time property names, like this:
      var obj:Object = {DYNAMICNAME1:"foo", DYNAMICNAME2:"bar"}

      (where DYNAMICNAME1 = "Fred" and DYNAMICNAME2 = "Barney", for example)

      Since a and b are not strings nor variables, either it can't be done or I can't understand how to do it.

      Can someone give me a nudge in the right direction please?
        • 1. Re: Contructing an Object with dynamic property names?
          levancho Level 3
          var o:object - new Object()

          o["+dinamicname+"] = somevalue;
          etc...
          • 2. Re: Contructing an Object with dynamic property names?
            Level 7

            "levancho" <webforumsuser@macromedia.com> wrote in message
            news:glu152$6o9$1@forums.macromedia.com...
            > var o:object - new Object()
            >
            > o["+dinamicname+"] = somevalue;
            > etc...

            Except skip the quotes and the plusses, because they'll give you an error.

            If dynamicName is a variable containing the name, use
            o[dynamicName] = 'foo';

            if dynamicName IS the name, use

            o['dynamicName'] = 'foo';


            • 3. Contructing an Object with dynamic property names?
              levancho Level 3
              yes correct, my bad , you can do quotes but it has to be double double,,
              like [""+obj]
              • 4. Re: Contructing an Object with dynamic property names?
                Level 7

                "levancho" <webforumsuser@macromedia.com> wrote in message
                news:glv5lh$kd8$1@forums.macromedia.com...
                > yes correct, my bad , you can do quotes but it has to be double double,,
                > like [""+obj+""]

                That doesn't add anything.


                • 5. Re: Contructing an Object with dynamic property names?
                  levancho Level 3
                  you are right it does not add anything to Object, but it does matter with Dictionary object
                  [obj] and [""+obj] are totally different keys

                  and also ""+ makes difference for value
                  = somevalue and = (somevalue+"") are totaly different.

                  ,I usually use +"" automaticelly regardles its Object or dictionalry if I want to make sure I want to use a string as the key and not object itself, again it does not matter for [Object Object] but it does for other hashes.
                  • 6. Re: Contructing an Object with dynamic property names?
                    Level 7

                    "levancho" <webforumsuser@macromedia.com> wrote in message
                    news:glv98g$oos$1@forums.macromedia.com...
                    > you are right it does not add anything to Object, but it does matter with
                    > Dictionary object
                    > [obj] and [""+obj] are totally different keys

                    If obj contains a string, adding quotes to the front of it results in
                    exactly the same thing. If it's a number, you are way better off using
                    toString() to make it clear what you're doing.

                    > and also ""+ makes difference for value
                    > = somevalue and = (somevalue+"") are totaly different.
                    >
                    > ,I usually use +"" automaticelly regardles its Object or dictionalry if I
                    > want to make sure I want to use a string as the key and not object itself,
                    > again it does not matter for [Object Object] but it does for other hashes.

                    All that does is calls the toString() method on the object, and if you
                    haven't provided one and it is REALLY an Object, it will be [Object object],
                    which is useless as a key, as all objects will have identical keys.


                    • 7. Contructing an Object with dynamic property names?
                      levancho Level 3
                      quote:

                      Originally posted by: Newsgroup User

                      "levancho" <webforumsuser@macromedia.com> wrote in message
                      news:glv98g$oos$1@forums.macromedia.com...
                      > you are right it does not add anything to Object, but it does matter with
                      > Dictionary object
                      > [obj] and [""+obj] are totally different keys

                      If obj contains a string, adding quotes to the front of it results in
                      exactly the same thing. If it's a number, you are way better off using
                      toString() to make it clear what you're doing.

                      > and also ""+ makes difference for value
                      > = somevalue and = (somevalue+"") are totaly different.
                      >
                      > ,I usually use +"" automaticelly regardles its Object or dictionalry if I
                      > want to make sure I want to use a string as the key and not object itself,
                      > again it does not matter for [Object Object] but it does for other hashes.

                      All that does is calls the toString() method on the object, and if you
                      haven't provided one and it is REALLY an Object, it will be [Object object],
                      which is useless as a key, as all objects will have identical keys.





                      difference is huge, in respect to Dictionary object if you do not +"" or (toString() it) you are mappping object to object if you do +"" you are mapping string to object,

                      here is example :

                      private var d:Dictionary = new Dictionary();
                      private var o:Object = new Object();
                      private var param:Object = new Object();
                      private var valueObj:Object = new Object();



                      private function init() : void
                      {
                      param["a"] = "secred value";
                      param["b"] = "second Secret Value";

                      d[param] = valueObj;
                      d[param+""] = valueObj;

                      o[param] = valueObj;
                      o[param+""] = valueObj;

                      for (var dkey:Object in d) {
                      trace ("[dictionary] top param: "+dkey+" = "+d[dkey]);
                      for (var _a:String in dkey) {
                      trace ("--suba"+_a+" = "+dkey[_a]);
                      }
                      }

                      for (var okey:Object in o) {
                      trace ("[object] top param: "+okey+" = "+o[okey]);
                      for (var _b:String in okey) {
                      trace ("--suba"+_b+" = "+okey[_b]);
                      }


                      }

                      }

                      output :

                      [dictionary] top param: [object Object] = [object Object]
                      [dictionary] top param: [object Object] = [object Object]
                      --subab = second Secret Value
                      --subaa = secred value
                      [object] top param: [object Object] = [object Object]


                      ---

                      P.S: mapping object to object is huge .


                      • 8. Re: Contructing an Object with dynamic property names?
                        AlHolden Level 1
                        Mommy, Daddy... please stop fighting!

                        I deliver the infant of your labors in it's first form: A draft function that takes the type of query that's returned by OpenBlueDragon and converts it to something usable as a data provider in Flex.
                        (OpenBD queries do not appear to have the same structure as CF queries)
                        The use of 'loopObj = new Object' inside the loop was my way of preventing all of the Array elements from all having the last value in the query. This may have something to do with whatever levancho was trying to explain?

                        function openBDQuery2FlexProvider(openBDQry:ObjectProxy):ArrayCollection
                        {
                        var loopObj:Object = new Object;
                        var ACresult:ArrayCollection = new ArrayCollection;
                        for (var x:int = 0; x < openBDQry['data'].length; x++)
                        {
                        loopObj = new Object;
                        for (var y:int = 0; y < openBDQry['columnList'].length; y++)
                        {
                        loopObj[openBDQry['columnList'][y]] = openBDQry['data'][x][y];
                        }
                        ACresult.addItem(loopObj);
                        }
                        return ACresult
                        }

                        I am very grateful for your 'nudge", and look forward to any other improvements you might offer. Sorry that the forum removed all my pretty formatting.
                        Alan K Holden
                        • 9. Re: Contructing an Object with dynamic property names?
                          Level 7

                          "levancho" <webforumsuser@macromedia.com> wrote in message
                          news:glvf76$2io$1@forums.macromedia.com...
                          >
                          quote:

                          Originally posted by: Newsgroup User
                          >
                          > "levancho" <webforumsuser@macromedia.com> wrote in message
                          > news:glv98g$oos$1@forums.macromedia.com...
                          > > you are right it does not add anything to Object, but it does matter
                          > > with
                          > > Dictionary object
                          > > [obj] and [""+obj] are totally different keys
                          >
                          > If obj contains a string, adding quotes to the front of it results in
                          > exactly the same thing. If it's a number, you are way better off using
                          > toString() to make it clear what you're doing.
                          >
                          > > and also ""+ makes difference for value
                          > > = somevalue and = (somevalue+"") are totaly different.
                          > >
                          > > ,I usually use +"" automaticelly regardles its Object or dictionalry if
                          > > I
                          > > want to make sure I want to use a string as the key and not object
                          > > itself,
                          > > again it does not matter for [Object Object] but it does for other
                          > > hashes.
                          >
                          > All that does is calls the toString() method on the object, and if you
                          > haven't provided one and it is REALLY an Object, it will be [Object
                          > object],
                          > which is useless as a key, as all objects will have identical keys.
                          >
                          >
                          >

                          >
                          > difference is huge, in respect to Dictionary object if you do not +"" or
                          > (toString() it) you are mappping object to object if you do +"" you are
                          > mapping
                          > string to object,
                          >
                          > here is example :
                          >
                          > private var d:Dictionary = new Dictionary();
                          > private var o:Object = new Object();
                          > private var param:Object = new Object();
                          > private var valueObj:Object = new Object();
                          >
                          >
                          >
                          > private function init() : void
                          > {
                          > param["a"] = "secred value";
                          > param["b"] = "second Secret Value";
                          >
                          > d[param] = valueObj;
                          > d[param+""] = valueObj;
                          >
                          > o[param] = valueObj;
                          > o[param+""] = valueObj;

                          Right here, you have a dictionary entry that is keyed to param, the object,
                          and another entry that is keyed to [Object object]. The latter is
                          completely useless, unless you don't have any other string keys that point
                          to different objects--any other calls to write to that dictionary key will
                          overwrite it.

                          So it might be working for you, but it will blow completely up if you have
                          more than one key set that way.

                          Try this:

                          private var d:Dictionary = new Dictionary();
                          private var o:Object = new Object();
                          private var param:Object = new Object();
                          private var valueObj:Object = new Object();

                          private function init():void {
                          d[o+'""] = "foo";
                          d[param+""]="bar;
                          }

                          Then put a break point at the end of init and see how many values you
                          actually have in d.

                          -Amy


                          • 10. Re: Contructing an Object with dynamic property names?
                            AlHolden Level 1
                            So, to my beginner's question above:
                            Is this the optimal way to do this?

                            function openBDQuery2FlexProvider(openBDQry:ObjectProxy):ArrayCollection
                            {
                            var loopObj:Object = new Object;
                            var ACresult:ArrayCollection = new ArrayCollection;
                            for (var x:int = 0; x < openBDQry['data'].length; x++)
                            {
                            loopObj = new Object;
                            for (var y:int = 0; y < openBDQry['columnList'].length; y++)
                            {
                            loopObj[openBDQry['columnList'][y]] = openBDQry['data'][x][y];
                            }
                            ACresult.addItem(loopObj);
                            }
                            return ACresult
                            }
                            • 11. Re: Contructing an Object with dynamic property names?
                              Level 7

                              "AlHolden" <webforumsuser@macromedia.com> wrote in message
                              news:glvt5m$j2n$1@forums.macromedia.com...
                              > Mommy, Daddy... please stop fighting!
                              >
                              > I deliver the infant of your labors in it's first form: A draft function
                              > that
                              > takes the type of query that's returned by OpenBlueDragon and converts it
                              > to
                              > something usable as a data provider in Flex.
                              > (OpenBD queries do not appear to have the same structure as CF queries)
                              > The use of 'loopObj = new Object' inside the loop was my way of preventing
                              > all
                              > of the Array elements from all having the last value in the query. This
                              > may
                              > have something to do with whatever levancho was trying to explain?
                              >
                              > function openBDQuery2FlexProvider(openBDQry:ObjectProxy):ArrayCollection
                              > {
                              > var loopObj:Object = new Object;
                              > var ACresult:ArrayCollection = new ArrayCollection;
                              > for (var x:int = 0; x < openBDQry['data'].length; x++)
                              > {
                              > loopObj = new Object;
                              > for (var y:int = 0; y < openBDQry['columnList'].length; y++)
                              > {
                              > loopObj[openBDQry['columnList'][y]] = openBDQry['data'][x][y];
                              > }
                              > ACresult.addItem(loopObj);
                              > }
                              > return ACresult
                              > }
                              >
                              > I am very grateful for your 'nudge", and look forward to any other
                              > improvements you might offer. Sorry that the forum removed all my pretty
                              > formatting.

                              It looks like your service already knows about Flex data types, so is there
                              any reason you're not just using RemoteObjects to return typed data? Also,
                              if it's not typed data and it's already in an ArrayCollection, I'm not sure
                              what it buys you to take the untyped objects and convert them to other
                              different untyped objects.


                              • 12. Re: Contructing an Object with dynamic property names?
                                AlHolden Level 1
                                I'm trying to get my BlazeDS working for RO's, but for now...

                                Via breakpoint, a ColdFusion query result looks like this in Eclipse:
                                [0]
                                COLUMNNAME | literalvalue
                                COLUMNNAME | literalvalue
                                [1]
                                COLUMNNAME | literalvalue
                                COLUMNNAME | literalvalue

                                But an OpenBD query result looks like this:
                                item
                                [0]
                                key | COLUMNNAME
                                val | literalvalue
                                [1]
                                key | COLUMNNAME
                                val | literalvalue

                                That's the only newbie way I can describe it. Sorry.
                                I've tried various flavors of Query2Array and Structures, but the end result looks the same, just different object types.

                                My function makes the later look like the former, and works. I just wondered if there was a lean or more gracious way to do it, Occam's razor and all that stuff.