Encountering an Angular object-array creation error. The structure of the data I need to display is as follows:
stats : [
{
name : 'x',
age : 20
},
{
name : 'y',
age : 10
},
];
In my Angular code, I have defined it like this:
stats: Array<{ name: string, age : number }> = [];
However, when attempting to assign a value like this:
this.stats[0]['name'] = 'x';
An error is thrown:
ERROR TypeError: Cannot set property 'name' of undefined
Note: I am not looking for an alternative solution as this specific representation and data binding are necessary for my project. Additionally, I cannot predefine the 'stats' array due to its dynamic nature and unknown size.