I'm relatively new to these programming languages and I know it may not be too difficult, but I am struggling to figure out how to dynamically access class instance variables.
My goal is to display an instance's variables in a table using Angular. I want to create a single function that can work with any class.
For example, let's consider a workbook class:
export class workbookEntity {
public name: string;
public creationDate: string;
constructor() {
this.name = 'my work book name';
this.creationDate = '2018/01/26';
}
}
Now, suppose I need to retrieve the names and values of the variables from an instance of this class within another class' function:
export class showClassComponent {
// some code here
whenSubmittedInHtmlForm(className: string): void {
// logic to iterate through instance
// className parameter will be 'workbookEntity'
}
// more code here
}
How would you iterate through the instance to retrieve each variable's name and value in order to achieve something like this?
[
{
name: 'name',
value: 'my work book name'
},
{
name: 'creationDate',
value: '2018/01/26'
}
]