0
votes

This is my following code:

private convertFormat(src: Array<any>): void {
for (let i of src) {
  this.selectedMonths.push({
    id: i,
    itemName: i
  })
};

this.selectedMonths = JSON.parse(JSON.stringify(this.selectedMonths));
console.log(this.selectedMonths);
}

However the ".push()" for when I try to push into the already declared Array<{}>, it gives me this error.

Unhandled Promise rejection: Cannot read property 'push' of undefined ; Zone: ; Task: Promise.then ; Value: TypeError: Cannot read property 'push' of undefined

1
selectedMonths : any[] = [] at the component class or ngOnInit this.selectedMonths = [];DrNio
I cant see your whole code but can you try this: private convertFormat = (src: Array<any>): void => {...} Maybe its a problem with 'this' not being your class's instanceDaniel
@Z.Bagley I mentioned in my question that I have declared it as "Array<{}>"its.david
@DrNio thanks! It worked! I declared it as 'selectedMonth: Array<{}>' but I should have made it this ' selectedMonth: Array<{}> = []'its.david
Or you can declare it like selectedMonths = new Array<{}>().ConnorsFan

1 Answers

1
votes

selectedMonths : any[] = [] at the component class or ngOnInit this.selectedMonths = [];