我正在尝试 Push
迭代值为 array
json_resp
是Json的回应 .
我的打字稿代码
export class hello
{
CategoryLst:any[]; //array
var catIndex,BillerIndex;
for(catIndex= 0; catIndex<= json_resp.category.length; catIndex++) {
var Clst=json_resp.category[0].categoryName;
this.CategoryLst.push(Clst);
}
}
在尝试执行其抛出错误时
ORIGINAL EXCEPTION: TypeError: Cannot read property 'push' of undefined
有什么我错过了吗?
2 回答
CategoryLst:any[]
只是指定array
的类型,但没有指定它,因此默认情况下该数组的值为undefined
.为了在同一个声明中初始化它,你应该这样做:
试试这个