I'm trying to dynamically pass in a value to the type variable in order to get any of the key-values from the testObj object. For example, when type = name, the testResult variable successfully returns 'bob'.
However, if I pass in type as address?.line1, it returns undefined. How can I get it to return the value correctly (in this case '13 test st') using the type variable? A similar principle applies to what I'm working on, so the below is the closest demo I could make.
Thank you.
var testObj = {
'name': 'bob',
'address': {
'line1': '13 test st'
}
}
// var type = 'name' // works as testResult correctly returns 'bob'
var type = 'address?.line1' // fails as testResult should return '13 test st'
var testResult = testObj?.[`${type}`]
address?.line1ontestObj