TypeError: Cannot set property 'name' of undefined

TypeError: Cannot set property 'name' of undefined

我正在尝试将 JSON 响应的某些值映射到另一个变量,但出现一些错误“无法设置 属性 未定义的名称”

export interface Data
{
   description: any;
   name : any;
}

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

   }