accessing object properties with variables

Solutions on MaxInterview for accessing object properties with variables by the best coders in the world

showing results for - "accessing object properties with variables"
Melia
21 Oct 2017
1var dogs = {
2  Fido: "Mutt",  Hunter: "Doberman",  Snoopie: "Beagle"
3};
4var myDog = "Hunter";
5var myBreed = dogs[myDog];
6console.log(myBreed); // "Doberman"
Alice
10 Mar 2018
1var someObj = {
2  propName: "John"
3};
4function propPrefix(str) {
5  var s = "prop";
6  return s + str;
7}
8var someProp = propPrefix("Name"); // someProp now holds the value 'propName'
9console.log(someObj[someProp]); // "John"