access object property variable javascript code example
Example 1: javascript dynamically access object property
var person ={"first_name":"Billy","last_name":"Riley"};
var property_name="first_name";
alert(person[property_name]); //Dynamically access object property with bracket notation
Example 2: Accessing Object Properties with Variables
var someObj = {
propName: "John"
};
function propPrefix(str) {
var s = "prop";
return s + str;
}
var someProp = propPrefix("Name"); // someProp now holds the value 'propName'
console.log(someObj[someProp]); // "John"