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/

3
  • do you know that objects in JavaScript are really just key-pairs and that you can[subscript] them with any expression? Commented Jan 8, 2015 at 10:20
  • possible duplicate of Dynamically access object property using variable Commented Jan 8, 2015 at 10:22
  • Having answered this myself ive also voted to close as there is at least one duplicate on SO Commented Jan 8, 2015 at 10:22

1 Answer 1

2

yes, use square bracket notation

var x = _buildings[chain].value;

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I will approve your answer after timeout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.