3

if I have an object, something like this

var o = {
  test : 1
}

and I would like to have a second object, one of it's keys should be the value of o.test. Something like this:

var o2 = {
   o.test : "bla"
}

I know this is not possible, but is there a better (cleaner) way to do it as I do this now?

Currently what I dow is this:

var o2 = {};
o2[o.test] = "bla"

I guess there is a better way for this?

1
  • o2[o.test] == o2[1] Commented Aug 20, 2013 at 6:54

3 Answers 3

7

I guess there is a better way for this?

Assuming I've understood your question correctly then no, not really. The way you've shown is the way to do it. There is no way to use a dynamic key inside the literal itself, so you have to declare it first and then assign the property separately:

var o2 = {};
o2[o.test] = "bla";

o2; // { 1: "bla" }

Update

The full details are given in the spec. Here's the grammar for object literal property identifiers:

PropertyName :
    IdentifierName
    StringLiteral
    NumericLiteral

The StringLiteral production is self-explanatory. Here's what the IdentifierName production does:

The production PropertyName : IdentifierName is evaluated as follows:

  1. Return the String value containing the same sequence of characters as the IdentifierName.

And for the NumericLiteral production:

The production PropertyName : NumericLiteral is evaluated as follows:

  1. Let nbr be the result of forming the value of the NumericLiteral.
  2. Return ToString(nbr).

You can see from this that it is not possible to use anything other than a string inside an object initialiser.

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

1 Comment

mhm, I hoped there was a better way for this, I could have saved a few lines of code then. Anyway, thank you.
0

Your Object properties can be any string

var o = {
  "whatever you want":1,
  "1":"numbers too"
}
o[2]="no numbers, convert to string"

console.log(o["whatever you want"]);//=1
console.log(o["1"]);//=numbers too
console.log(o[new String(2)]);//=no numbers, convert to string
// as James commented, you don't have to convert to string
// I usually do that as to not confuse the object with an array
console.log(o[2]);//=no numbers, convert to string

If all your "property names" are positive integers or 0 you could use an array:

var arr=new Array();//or var arr=[];
var b=22;
arr[10000]="hi";
arr[b]="value";
// normally you would do for(var i=0;len=arr.length;i<len;i++){...arr[i]
// but because there may be many empty spots it's quicker to do:
for(thing in arr){
  if(arr.hasOwnProperty(thing)){
    console.log(thing  + " is " + arr[thing]);
  }
}

3 Comments

I wouldn't say any string, as the declaration can only use constants. As demonstrated, once initialised the object can use any string for a property.
@RGraham correct, you can't declare number values as object names in the "body" of the object literal so I guess the answer to the OP's question is no.
@HMR - You don't have to convert the number to a string. That's done automatically. See the PropertyName : NumericLiteral production from the grammar in my answer.
0

This being javascript, there is almost always a workaround for doing what you want.

There is no syntax for doing what you want but you can certainly write a function to do it:

function make () {
    // Optional sanity check:
    if (arguments.length % 2) throw "Number of arguments should be even!";

    var obj = {};
    for (var name=0,val=1; name < arguments.length; name+=2,val+=2) {
        obj[arguments[name]] = arguments[val];
    }
    return obj;
}

Now you can write this:

var o2 = make(
    o.test,        'bla',
    'another_key', 'another_val'
);

Comments

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.