Answers for "Accessing Object Properties with Variables"

1

Accessing Object Properties with Variables

var dogs = {
  Fido: "Mutt",  Hunter: "Doberman",  Snoopie: "Beagle"
};
var myDog = "Hunter";
var myBreed = dogs[myDog];
console.log(myBreed); // "Doberman"
Posted by: Guest on July-19-2020
1

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"
Posted by: Guest on July-19-2020

Code answers related to "Accessing Object Properties with Variables"

Code answers related to "Javascript"

Browse Popular Code Answers by Language