Reputation: 2516
I'm trying to push
iterated value to array
json_resp
is Json response.
my Typescript code
export class hello {
CategoryLst:any[];
var catIndex,BillerIndex;
for(catIndex = 0; catIndex <= json_resp.category.length; catIndex++) {
var Clst = json_resp.category[0].categoryName;
this.CategoryLst.push(Clst);
}
}
while trying to execute its throwing error as
ORIGINAL EXCEPTION: TypeError: Cannot read property 'push' of undefined
is there something am i missing ??
Upvotes: 0
Views: 3822
Reputation: 44659
CategoryLst:any[]
is just specifying the type of the array
, but not assigning it, so by default the value of that array would be undefined
.
In order to initialize it in the same declaration, you should do it this way:
CategoryLst:any[] = [];
Upvotes: 3