0

我正在尝试将push价值迭代到array

json_resp是 Json 响应。

我的打字稿代码

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);
  }
}

在尝试执行其抛出错误时

原始异常:TypeError:无法读取未定义的属性“推送”

有什么我想念的吗?

4

2 回答 2

4

尝试这个

CategoryLst:any[] = [];
于 2016-06-22T10:32:58.153 回答
3

CategoryLst:any[]只是指定 的类型array,而不是分配它,所以默认情况下该数组的值是undefined.

为了在同一个声明中初始化它,你应该这样做:

CategoryLst:any[] = [];
于 2016-06-22T10:36:09.197 回答