Transistor
Transistor

Reputation: 313

Retrieve a value from a Javascript object

How do I retrieve a value from the object below?

var recipe= {};
recipe[0, 'prodmem_active.tpoint_prod[0].setpoint'] = 1600;
recipe[0, 'prodmem_active.tpoint_prod[1].setpoint'] = 1300;
recipe[1, 'prodmem_active.tpoint_prod[0].setpoint'] = 1600;
recipe[1, 'prodmem_active.tpoint_prod[1].setpoint'] = 1300;
recipe[2, 'prodmem_active.tpoint_prod[0].setpoint'] = 1500;
recipe[2, 'prodmem_active.tpoint_prod[1].setpoint'] = 1200;

e.g. alert(recipe[1]['prodmem_active.tpoint_prod[1].setpoint']) doesn't work.

Upvotes: 0

Views: 39

Answers (1)

Quentin
Quentin

Reputation: 943217

You have to assign the value correctly in the first place.

The comma operator evaluates as its right hand side.

This:

recipe[0, 'prodmem_active.tpoint_prod[0].setpoint'] = 1600;

Means the same as:

recipe['prodmem_active.tpoint_prod[0].setpoint'] = 1600;

You are trying to create a new object and then assign a value to one of the new objects properties.

recipe[0] = {};
recipe[0]['prodmem_active.tpoint_prod[0].setpoint'] = 1600;

Upvotes: 2

Related Questions