Maxim Veksler
Maxim Veksler

Reputation: 30222

Using parameter names from variable in json object

Trying to work out a map reduce on mongo that would use field value as parameter name of the emitted object.

To simply what I'm tying to do is the following:

emit_object = {}
param_name = "param1"
param_value = 1
emit_object.param_name = param_value

The object I wish to construct is:

{ "param1" : 12 }

Yet the being constructed is the following:

{ "param_name" : 12 }

Does JS support this "dynamic" behavior of object construction? (Mongo uses SeaMonkey JS engine, if that's relevant).

Thank you, Maxim.

Upvotes: 4

Views: 9293

Answers (2)

aorlando
aorlando

Reputation: 668

emit_object[param_name]

The name is "bracket notation", there is also "dot notation to access" Object in javascript

Upvotes: 3

J.S. Taylor
J.S. Taylor

Reputation: 761

emit_object[param_name] = param_value

Upvotes: 10

Related Questions