0

I'm not sure if I'm correct with topic title, so sorry about that. I've got JS object '_buildings', which structure looks like this:

_buildings : {
        laboratory : {
            exist : false,
            value : 1000,
        },
        office : {
            exist : false,
            value : 500,
        },
}

Is it possible to access object somehow using this method:

var chain = 'laboratory'; //it could be 'office' or any other building name
var value = _buildings.chain.value;

Point is, I need to access object param while using variable in chain. Is it possible?

JSFiddle: http://jsfiddle.net/3k5anjjj/

Dazvolt
  • 697
  • 2
  • 10
  • 22
  • do you know that objects in JavaScript are really just key-pairs and that you `can[subscript]` them with any expression? – The Paramagnetic Croissant Jan 08 '15 at 10:20
  • possible duplicate of [Dynamically access object property using variable](http://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Jamiec Jan 08 '15 at 10:22
  • Having answered this myself ive also voted to close as there is at least one duplicate on SO – Jamiec Jan 08 '15 at 10:22

1 Answers1

2

yes, use square bracket notation

var x = _buildings[chain].value;

Updated fiddle: http://jsfiddle.net/3k5anjjj/1/

Jamiec
  • 133,658
  • 13
  • 134
  • 193