for example:
object = {
name: "Mike",
age: 15
}
object.age // returns 15 right?
But I want to pass the 'age' key as a variable name:
object = {
name: "Mike",
age: 15
}
const age = 10.toString()
How do I get the result of something like this:
object.`${age}`
Try
object["age"]
or
var propName='age'
object[propName]
You can simply do this by
object[age]
Properties can be accessed by either . (dot notation) or via brackets []
that is:
object[key_name]
// where key_name is a variable
object["some_key"] // {"some_key":"value"} or {some_value:"value"}
object[19] // {19:"some_value"}