I am building some forms that need to handle entities that might have nested objects. So I need the interface to accept as the field key either a string or an array of strings with each piece of the path to the value (like the example below).
const obj = {
name: "John",
role: {
id: 1,
name: "admin"
}
}
const key1 = 'name'
const key2 = ['role', 'name']
function getValueByKey (key, obj) {
if (Array.isArray(key)) {
//Get value if key is array.
} else {
return obj[key]
}
}
console.log(getValueByKey(key1, obj))
//Should output "John"
console.log(getValueByKey(key2, obj))
//Should output "admin"