I want to access a Javascript Object dynamicly.
Example:
example: {
name: "dev.pus",
year: 2012,
os: "linux"
}
This isn't anything new. Now you normaly can access properties of the "example" with:
console.log(example.name);
// or
console.log(example.year);
But what is if I want to take the attribute dynamicly? For example, another var (lets assume the user sets it) should decide which property we want:
var = "name";
console.log(example.var); // error
console.log(example[var]); // error
What is the way to go?
example[variable]should work. You're just choosing the worst name in the history of variable names...varis a reserved keyword in Javascript.