2

Please check code below. Here all variable values are static.

var o = { level_a:{}, level_b:{}, . . . .};

var levelVar = "b";

var selected_tab = 'level'+'_'+levelVar; \\level_b

var result = o.selected_tab;

Here you can see var o is object and var levelVar and selected_tab are string. Now I expect I should get value of o.level_b inside result, but its not working becuse we can not concat string to object.

Please help.

1 Answer 1

8

Use this notation :

result = o[selected_tab];

More generally, when you have var obj={a:'b'}, you can access the property a using both obj.a and obj['a'].

Here's a MDN reference about the use of objects and properties.

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

2 Comments

I am so fool . .. never thought in that way . . .anyways thanks !
don't forget to check with o.hasOwnProperty(selected_tab) to avoid erros ;)

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.