Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Typescript数组推送仅显示最新项目_Javascript_Angular_Typescript - Fatal编程技术网

Javascript Typescript数组推送仅显示最新项目

Javascript Typescript数组推送仅显示最新项目,javascript,angular,typescript,Javascript,Angular,Typescript,我有一个从API获取的列表,如: { "content": [ { "trainingClassId": 1, "examCode": "my exam 1", "address": { "addressId": 1, "addressValue": "abc 1213" }, "description": "only test", "classId": null, "

我有一个从API获取的列表,如:

{
  "content": [
    {
      "trainingClassId": 1,
      "examCode": "my exam 1",
      "address": {
        "addressId": 1,
        "addressValue": "abc 1213"
      },
      "description": "only test",
      "classId": null,
      "startDate": 1511110800000,
      "endDate": 1513702800000,
     "examDate": 1511542800000

    },
    {
      "trainingClassId": 2,
      "examCode": "my exam 2",
      "address": {
        "addressId": 1,
        "addressValue": "abc 1213"
      },
      "description": "only test",
      "classId": null,
      "startDate": 1511110800000,
      "endDate": 1513702800000,
     "examDate": 1511542800000
    }
  ],
  "last": true,
  "totalElements": 2,
  "totalPages": 1,
  "size": 20,
  "number": 0,
  "first": true,
  "sort": null,
  "numberOfElements": 2
}
我想将long转换为date,因此我创建了一个绑定数据的对象:

export class myApp {
    id: number;
    classId: string;
    trainingDate: string;
    examDate: string;

}
在我的ts上:

listData = new Array();

  app_unit:myApp= new myApp();
  listApp:any[];

ngOnInit() {
    this.getAllTrainingClass();

  }
 async getAllTrainingClass(): Promise<void>{
     await this.traningClassService.getdata().then(data =>this.listApp = data);

     for(let ls of this.listApp){


        this.app_unit.classId= ls.classId;
        this.app_unit.examDate=this.convertTimestampToDate(ls.examDate);
        this.app_unit.trainingDate=this.convertTimestampToDate(ls.startDate) +'-'+this.convertTimestampToDate(ls.endDate) ;

       this.listData.push(this.app_unit);


     }
listData=newarray();
app_单位:myApp=新myApp();
listApp:任何[];
恩戈尼尼特(){
这个.getAllTrainingClass();
}
异步getAllTrainingClass():承诺{
等待这个.traningClassService.getdata()。然后(data=>this.listApp=data);
for(让ls of this.listApp){
this.app_unit.classId=ls.classId;
this.app_unit.examDate=this.convertTimestampToDate(ls.examDate);
this.app_unit.trainingDate=this.convertTimestampToDate(ls.startDate)+'-'+this.convertTimestampToDate(ls.endDate);
this.listData.push(this.app_单元);
}
的控制台日志有2项,但它是最新的项,如下所示:

{0:myApp 身份证号码:2 classId:null 考试日期:“2018年1月5日” 培训日期:“2017年11月5日-2017年12月31日” }, {1:myApp 身份证号码:2 classId:null 考试日期:“2018年1月5日” 培训日期:“2017年11月5日-2017年12月31日”

请告诉我。

问题是因为“this.app\u unit”指向相同的内存位置,您正在将相同的对象插入数组,所有对象都指向相同的内存位置

在for循环内部。
因此,每个循环都会创建一个新的对象实例。

我认为问题在于,this.app\u unit.指向相同的内存位置,即您将相同的对象插入数组,所有对象都指向相同的内存位置。put,var app\u unit:myApp=new myApp();位于for循环内。请尝试在循环内创建它。但显示错误:无法分配给“myApp”,因为它不是变量。Np,我忘记了声明
  var app_unit:myApp= new myApp();