Dazvolt
Dazvolt

Reputation: 697

Access js object using variable

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/

Upvotes: 0

Views: 46

Answers (1)

Jamiec
Jamiec

Reputation: 136104

yes, use square bracket notation

var x = _buildings[chain].value;

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

Upvotes: 2

Related Questions