In my application, I am working with a user object that looks like this:
let user = {name: "John", dob:"1995-10-15", metadata: {}}
The metadata
property of the user object is initially empty. I want to add a new property to the metadata
object based on user input. For example:
<input [(ngModel)]="user.metadata.childrenNumber" placeholder="Enter number of children"></input>
Everything is functioning correctly so far. However, I have a list of properties I want to create stored in an array:
let metaDataOptions = ['childrenNumber', 'workStatus', 'education'];
How can I dynamically reference the property name from this array? I attempted something like this:
// i represents an index from ngFor loop, which could be 0, 1, or 2
<input [(ngModel)]="user.metadata.metaDataOptions[i]" placeholder="Enter number of children"></input>
Unfortunately, this approach did not work as expected. How can I achieve the desired result?