object - javascript getProperty -


this question has answer here:

i'm complete beginner in regards javascript , coding in general. i'm planning attend coding boot camp soon, part of requirements entry need learn basics first. i've been getting through first few problems in regards if/else quite easily, ran 1 requires me return value that's assigned key. function called getproperty , i'm stuck. unfortunately, each of other questions build on know 1 concept such as: addproperty , removeproperty. appreciated allow me continue studies! below have currently. keeps returning "should return value of property located in object @ passed in key" every time try run test on code.

i know question simple solve, there many online resources clear answer.

    var obj = {key: 'value'};      function getproperty(obj, key) {         var o = obj.key;         return(o);     }      console.log(getproperty); 

for dynamic attribute names need use bracket [] notation instead of dot notation:

var o = obj[key]; return o 

thanks @gaby pointing out need call function valid arguments:

console.log(getproperty(obj, 'key')); 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -