I want to dynamically fetch the value of an object. Here is the object:
myObj = {
person1:{name:"John Doe", age:"46"},
person2:{name:"Peter Deer", age:"36"},
person3:{name:"Ben Boar", age:"21"}
}
I have a function to fetch a desired name from the objects. Here's the function:
showPersonName(objKey: number) {
console.log(this.myObj + 'person' +objKey.name);
}
The result I want if I pass 1 as objKey is to display John Doe, but Visual Studio Code shows
Property 'name' does not exist on type 'number'.
Please, what can I do? I've had a hard time with this.