8

In JavaScript, if you have the following code:

  var map_id = 100;
  var myobj = {};
  myobj[map_id] = 6;
  var myobj2 = { map_id : 6 };
  console.log(myobj, myobj2);

The console output is as follows:

{ '100': 6 } { map_id: 6 }

Questions:

  1. Why does JavaScript syntax work differently in these two different cases - why is they key in myobj2 set to the literal map_id rather than 100? What is the reasoning behind having this difference?
  2. Is there any way to set the key to the value of the map_id variable in a compact, one-line way, rather than having to define the object separately first?

Thanks.

Richard
  • 31,629
  • 29
  • 108
  • 145

4 Answers4

8

Is there any way to set the key to the value of the map_id variable in a compact, one-line way, rather than having to define the object separately first?

No. Sorry.

(Unless you count putting both statements on one line separated by a semicolon, which I don't.)

Why does JavaScript syntax work differently in these two different cases - why is they key in myobj2 set to the literal map_id rather than 100? What is the reasoning behind having this difference?

Good question. I don't know, and I've never been able to think of a good reason. I think it would make much, much more sense if the key names were treated like expressions the same as pretty much everything else in JavaScript, i.e., as a literal if quoted ({ "map_id" : 6 }), and as a variable if not quoted ({map_id : 6 }).

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
1

One-line way

Object.defineProperty({}, map_id, { value: 6, enumerable: true });

ES6

{ [map_id]: 6 }
Mario Negrete
  • 240
  • 2
  • 7
  • Related solution with explanation about the ES6 way: http://stackoverflow.com/questions/19837916/creating-object-with-dynamic-keys – dmonopoly Dec 19 '16 at 17:28
1

myobj[map_id] means to access a property which name is the value of map_id (as an expression).

In your case the value of map_id is 100. All object properties must be Strings or valid JavaScript identifiers - so it is transformed into String '100' and put as object's property.

In the case of var myobj2 = { map_id : 6 }; the JavaScript interpreter sees that map_id is a valid identifier and doesn't expect it to be an expression - so it is not transformed into anything.

EDIT:

as in regards to your second question and assuming that you want the name of the property to be dynamic (as in dependent on the value of some expression), but also write:

var myobj2 = { map_id : 6 };

and have map_id replaced with "its" (its not really its value - the two identifiers just happen to have the same name - one is an object property and the other one is a variable) value to produce:

{ '100': 6 }
  • the answer is no, you would have to do it through [].
ZenMaster
  • 12,363
  • 5
  • 36
  • 59
  • 1
    The question is _why_ does it see `map_id` as a key literal and not take it as a variable? – nnnnnn Oct 26 '11 at 10:01
  • Hmm...I didn't see your edit until after I commented (mere comments don't make the page reload), but even so you still didn't explain _why_ JS works that way. My paraphrase of the original question would be "Why did JS's designer(s) decide that object literal syntax would always treat the keys as literals even when not quoted?" – nnnnnn Oct 26 '11 at 10:10
  • @nnnnnn I see. The question was "what is the reasoning then" makes more sense to me now. Probably misinterpretation on my part. – ZenMaster Oct 26 '11 at 10:12
  • @ZenMaster thanks for the answer, but yes, I was looking for an explanation of *why* JavaScript treats keys this way... – Richard Oct 26 '11 at 10:17
  • @Richard why would you accept nnnnnn's answer then and not mine? Just out of curiousity... because neither provided you an answer to your 2 question. – ZenMaster Oct 26 '11 at 10:39
  • We both provided an answer to question 2, but unfortunately the answer was "no". Still, I'm happy to give you a +1... – nnnnnn Oct 26 '11 at 10:51
  • @nnnnnn just as I am to you, but that's not really what I asked Richard. I was more interested in a thought process - after all it is entirely possible that kangax or bobince came along and provided an answer. I was intent on checking back :) – ZenMaster Oct 26 '11 at 12:40
0

Within an object literal, keys are symbols not variables. I do not believe wrapping keys in double-quotes converts them to strings. Rather the quotes are used to create symbols which can contain spaces or other non-alphanum values, like {"\n":"this is a carriage-return"}

So in your example, 'map_id' used as a key within an object literal has nothing to do whatsoever with the variable 'map_id', other than they both use the same sequence of characters for their names.

Rob Raisch
  • 17,040
  • 4
  • 48
  • 58