[英]TypeError: Cannot set property 'name' of undefined
我正在嘗試 map JSON 的一些值響應另一個變量,但收到一些錯誤“無法設置未定義的屬性名稱”
export interface Data
{
description: any;
name : any;
}
內部主要 class 定義了以下數據
actionData : any;
action:Data[]=[];
getData()
{
this.spref.getNewData().subscribe(
response => {
this.actionData = response;
for(let i=0;i<this.actionData.length;i++)
{
this.action[i].name = this.actionData[i].name;
this.action[i].description = this.actionData[i].description;
}
})
},
error => {
console.log('Failure: ', error);
}
);
}
此格式的 actionData 響應
[{
description: "pqrs"
jsonType: "com.iti.dexcenter.common.object.NewData"
name: "abc"
value: "xyz"
}]
我希望動作數據以這種格式存儲
[{
description: "pqrs"
name: "abc"
}]
提前致謝!
如果未初始化,則action[i]
未定義。 因此,在為其設置任何屬性之前,您需要對其進行初始化,如下所示:
actionData : any;
action:Data[]=[];
getData()
{
this.spref.getNewData().subscribe(
response => {
this.actionData = response;
for(let i=0;i<this.actionData.length;i++)
{
this.action[i] = {
name: this.actionData[i].name;
description: this.actionData[i].description;
}
}
})
},
error => {
console.log('Failure: ', error);
}
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.