Access Object Property Dynamically
Sometimes you don’t know the exact property name when writing code.
In that case, you can use bracket notation to access a property dynamically using a string.
Use the syntax
Write a function
Example:
In that case, you can use bracket notation to access a property dynamically using a string.
Use the syntax
obj[key]
instead of obj.key
.Write a function
getProp
that takes an object and a key (string), and returns the value of the matching property. Example:
getProp({a: 42}, 'a')
should return 42
.Premium
JavaScript
Console