0

I have an Angular Model and I am trying to push it to an array to create repeater fields. The array is then iterated to create html fields in a component. However, while pushing to the array, an undefined error is thrown enter image description here.

Here is the Model code:

export class TierModel { feeName: string; feeDescription: string; unit: string; tierStart: number; tierEnd: number; rate: number; }; 

And here is the code that pushes Model to array:

addForm() { const obj = new TierModel() obj[0].feeName = 'test'; obj[0].unit = 0; this.dataarray.push(obj); } 

The aim is to use an index with the object and then push it to the array so that any modification is to be done before pushing it to the array would be done here using index.

Any help would be more than

5
  • have you considered the initial situation where this.dataarray is empty? newlen would be -1 and this.dataarray[0] would be undefinedCommentedFeb 13, 2023 at 8:21
  • @Zerotwelve I have modified my question but the error is the same. Could you please check now?CommentedFeb 13, 2023 at 8:26
  • it makes no sense to access obj with an index since it isn't an array. use obj.feeName = 'test'; or `obj["feeName"] = 'test'; insteadCommentedFeb 13, 2023 at 8:38
  • @Zerotwelve But what should I do if I need to push multiple feeName and unit?CommentedFeb 13, 2023 at 8:56
  • then you call addForm() again and another object will be created/added. maybe you should explain you problem/goal more preciselyCommentedFeb 13, 2023 at 9:39

2 Answers 2

1

try the following

addForm() { const obj = new TierModel() obj.feeName = 'test'; obj.unit = 0; this.dataarray.push(obj); } 

your const obj is an object so there is no need for obj[0].

2
  • Thank you so much for your reply but what if I would have to push multiple feeName and unit to the same array with different values? Should I have to use indexing or will it work without indexing as you have suggested?CommentedFeb 13, 2023 at 8:55
  • for multiple values you can create new TierModel and again push it to the array, there is no need for indexing
    – NTP
    CommentedFeb 13, 2023 at 8:57
0

This is all messed up,

There is no obj[0], obj is not an array, its an instance of class, that has no constructor.

And pay attention to types, you are defining unit as string but giving it value of number.

Additionally, you dont even need to export class for this. Creating something with new <class>() is confusing when in reality its just an object. If you are declaring object you need to see {}, this is how you make it understandable for anyone on the first look.

This is how you do what you are trying to do

export interface TierModel { feeName: string; feeDescription: string; unit: number; tierStart: number; tierEnd: number; rate: number; } export class AppComponent implements OnInit { objArray: TierModel[] = []; constructor() {} ngOnInit(): void { this.addForm(); console.log(this.objArray); } addForm() { let obj = <TierModel>{}; obj.feeName = 'aaa'; obj.unit = 1; this.objArray.push(obj); } } 
1
  • Thank you for the reply. Accepting your answer.CommentedFeb 13, 2023 at 10:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.