【发布时间】:2019-01-10 02:34:42
【问题描述】:
我想对数组进行排序并将获取的值存储到一个新数组中以供打印。我尝试使用 push(),但出现错误并显示“无法读取未定义的属性 'push'”。
`
this.dataService.userprofile().subscribe((data:any) => {
let profile:any[]=data.profiles;
for(let index=0;index<profile.length;index++) {
const element = profile[index];
const today: number = Date.now();
var b = Math.abs(today - Date.parse(element["dob"]));
element["dob"] = Math.floor(b / (1000 * 3600 * 24 * 365.25));
if (element["active"]=="N"){
this.row.push(element);
}}
this.rowData = this.row;//ag-grid row
console.log(this.rowData)
})
`
【问题讨论】:
-
this.row是否在您的函数上下文中以任何方式初始化? -
this.row = (this.row || []).concat(element); -
@Paqman 我认为
row是一个局部变量,用于存储elements 的数组并填充初始化的this.rowData.. 在我的回答中,我建议直接使用this.rowData分配Array.prototype.reduce()的结果
标签: javascript angular push