I am outputting data using dot notation and I am trying to convert that dot notation to bracket notation to be able to access the value for that property programatically.
What is the best way to be able to access the nested property of data.person.fullName
via data['person']['fullName']
This example is vanilla JS but I it will be TypeScript. I appreciate you looking.
const data = {
firstName: 'Jane Doe',
lastName: 'Doe',
person: {
fullName: 'Jane Doe'
}
}
function outputValue(path) {
console.log('path passed:', path)
console.log('value:', data[path])
const isNested = new RegExp('.').test(path)
if (isNested) {
const nestedPath = path.split('.')
nestedPath.forEach((path) => console.log('path:',path))
}
console.log('--------------------------------')
}
outputValue('firstName')
outputValue('person.fullName')