general js-question

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

general js-question

Tobias Koller (GERMO GmbH)

 

Hi list,

 

one simple question:

 

I want to define an object like:

 

var test = {

      “abc” : {

            “test1” :{

           

},

“test2” :{

           

}    

      },

     

      “myTest” :{

            “x1” : {

 

            }

      }

}

 

 

That is just an example ;) .

 

Now I want to use variables instead of real text for the identifiers….is this possible?

 

Of course I could make it like:

 

var test = {};

var var1 = “abc”;

var var2 = “myTest”;

 

test[ var1 ] = {

            “test1” :{

           

},

“test2” :{

           

}    

      };

 

test[ var2 ] = :{

            “x1” : {

 

            }

      }

 

 

 

But this is not really how I want to make it.

Is there no other way to use variables for identifiers?

 

I’m sure some of you JS-Gurus can help me here ;)

 

 

Thanks

Tobias

 

 

 

 


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: general js-question

thron7
Tobias Koller (GERMO GmbH) wrote:

>
> Hi list,
>
> one simple question:
>
> I want to define an object like:
>
> var test = {
>
> “abc” : {
>
> “test1” :{
>
> },
>
> “test2” :{
>
> }
>
> },
>
> “myTest” :{
>
> “x1” : {
>
> }
>
> }
>
> }
>
> That is just an example ;) .
>
> Now I want to use variables instead of real text for the
> identifiers….is this possible?
>
> Of course I could make it like:
>
> var test = {};
>
> var var1 = “abc”;
>
> var var2 = “myTest”;
>
> test[ var1 ] = {
>
> “test1” :{
>
> },
>
> “test2” :{
>
> }
>
> };
>
> test[ var2 ] = :{
>
> “x1” : {
>
> }
>
> }
>
> But this is not really how I want to make it.
>
> Is there no other way to use variables for identifiers?
>

I'm afraid no. Object literal syntax uses identifiers or strings in key
positions, so whatever you put there will be interpreted "as is". I
think there is no built-in way to evaluate the identifier first. So what
is left to you is to do it yourself, either by defining the map as a big
string with '%N' in it, and then run qx.lang.String.format() over it to
interpolate the variables and then eval() the string. Or write a
processing function that takes the initial object, traverses it, and
replaces keys with their eval'd value wherever possible (which might
make for a nice generic function). So with vars like
a1='hugo';
a2='berti';
t = {a1:1, a2;2};
you would do something like
for (x in t){
if (eval(x)){t[eval(x)]=t[x]; delete t[x]} // needs error handling, but
you get the idea
}
which should leave you with
t = {'hugo':1, 'berti':2}

HTH,
Thomas

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|

Re: general js-question

Tobias Koller (GERMO GmbH)
In reply to this post by Tobias Koller (GERMO GmbH)
Thanks for the quick answer!
I will try it ;)


Thanks
Tobias

-----Ursprüngliche Nachricht-----
Von: [hidden email] [mailto:[hidden email]] Im Auftrag von thron7
Gesendet: Dienstag, 1. Juli 2008 14:36
An: qooxdoo Development
Betreff: Re: [qooxdoo-devel] general js-question

Tobias Koller (GERMO GmbH) wrote:

>
> Hi list,
>
> one simple question:
>
> I want to define an object like:
>
> var test = {
>
> "abc" : {
>
> "test1" :{
>
> },
>
> "test2" :{
>
> }
>
> },
>
> "myTest" :{
>
> "x1" : {
>
> }
>
> }
>
> }
>
> That is just an example ;) .
>
> Now I want to use variables instead of real text for the
> identifiers....is this possible?
>
> Of course I could make it like:
>
> var test = {};
>
> var var1 = "abc";
>
> var var2 = "myTest";
>
> test[ var1 ] = {
>
> "test1" :{
>
> },
>
> "test2" :{
>
> }
>
> };
>
> test[ var2 ] = :{
>
> "x1" : {
>
> }
>
> }
>
> But this is not really how I want to make it.
>
> Is there no other way to use variables for identifiers?
>

I'm afraid no. Object literal syntax uses identifiers or strings in key
positions, so whatever you put there will be interpreted "as is". I
think there is no built-in way to evaluate the identifier first. So what
is left to you is to do it yourself, either by defining the map as a big
string with '%N' in it, and then run qx.lang.String.format() over it to
interpolate the variables and then eval() the string. Or write a
processing function that takes the initial object, traverses it, and
replaces keys with their eval'd value wherever possible (which might
make for a nice generic function). So with vars like
a1='hugo';
a2='berti';
t = {a1:1, a2;2};
you would do something like
for (x in t){
if (eval(x)){t[eval(x)]=t[x]; delete t[x]} // needs error handling, but
you get the idea
}
which should leave you with
t = {'hugo':1, 'berti':2}

HTH,
Thomas

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel