Accessing javascript object with string value

javascript, javascript-objects

Solution

Do it this way:

console.log(object.test[variable]);

Doing it with dots is using literal attribute names. I.e., `object.test.value` equates to `object.test['value']`.

Problem

I don't understand why can't i access values like this: ``` object = { test:{ value: "Hello world" } } variable = "value"; //this gives me "Hello world" console.log(object.test.value); //this gives me undefined error console.log(object.test.variable); ``` By now i can understand that it can't be done this way, but i still need to give some value to the variable and then use that variable to access object values.

Original source

Related problems