简体   繁体   English

如何在 Typescript 中动态填充 Arrays 数组

[英]How to Dynamically Populate an Array of Arrays in Typescript

I'm coming from .net and MVC to Angular.我来自 .net 和 MVC 到 Angular。 I need to create an array object that is made up of multiple array objects.我需要创建一个由多个数组对象组成的数组 object。 I know how to do this in.Net but not Angular.我知道如何在.Net 中执行此操作,但不知道 Angular。 This is what I have in.Net for my data models:这就是我在.Net 中为我的数据模型所拥有的:

public class ProgramScorecardModel
{
    public string programId { get; set; }
    public DateTime? reportingDate { get; set; }
    public string programName { get; set; }
    public IList<AB_ViewModel> AB_ViewModel { get; set; }
    public IList<CD_ViewModel> CD_ViewModel { get; set; }
}

public class AB_ViewModel
{
    public string rating { get; set; }
    public DateTime? reportingDate { get; set; }
    public string comments { get; set; }
}
public class CD_ViewModel
{
    public string rating { get; set; }
    public DateTime? reportingDate { get; set; }
    public string comments { get; set; }
}

These data models are populated via queries to a database.这些数据模型是通过对数据库的查询来填充的。 I need to do the same thing in Angular.我需要在 Angular 中做同样的事情。 I created interfaces like this to represent the data:我创建了这样的接口来表示数据:

interface ProgramScorecardModel {
    programId: string;
    reportingDate: string;
    programName: string;
    AB_ViewModel: any;
    CD_ViewModel: any;
}

interface AB_ViewModel {
    rating: string;
    reportingDate: string;
    comments: string;
}
interface CD_ViewModel {
    rating: string;
    reportingDate: string;
    comments: string;
}

I've then tried to do the following in my function然后我尝试在我的 function 中执行以下操作

let ProgramScorecard: ProgramScorecardModel[]=[]
    proglist.forEach(async (p, index) => {
        ProgramScorecard[index].programId = p.programId;
        ProgramScorecard[index].programName = p.programName;

        let AB_data = await this.GetMeasureForScorecard(p.programid,reportingdate);
        let CD_Data= await this.GetMeasureForScorecard(p.programid,reportingdate);
        
        ProgramScorecard[index].AB_ViewModel= AB_data ;
        ProgramScorecard[index].CD_ViewModel = CD_Data;
        
    });

The end result will be a ProgramScorecard with multiple programs and for each program, arrays of the AB and CD data.最终结果将是一个包含多个程序的 ProgramScorecard,对于每个程序,AB 和 CD 数据的 arrays。 I keep getting errors trying to put anything into the ProgramScorecard.尝试将任何内容放入 ProgramScorecard 时,我不断收到错误消息。 How do I do this?我该怎么做呢?

EDIT: In regard to the questions:编辑:关于问题:

  1. This is the error: TypeError: Cannot set properties of undefined (setting 'programId')这是错误:TypeError:无法设置未定义的属性(设置'programId')
  2. Proglist is a an array of (programId,programName,reportingdate) Proglist 是一个包含 (programId,programName,reportingdate) 的数组
  3. Index is the index of the element in the array, it is a property of the foreach. index是数组中元素的索引,是foreach的一个属性。
  4. I understand the programscorecard is empty.我知道programscorecard 是空的。 How do I populate it using this foreach.如何使用这个 foreach 填充它。

This is what I ended up doing:这就是我最终做的事情:

let ProgramScorecard: ProgramScorecardModel[] = []
for (const p of proglist) {
  const [AB_data, CD_Data] = await Promise.all([
  this.GetMeasureForScorecard(p.programId, reportingdate, "GetData1"),
  this.GetMeasureForScorecard(p.programId, reportingdate, "GetData2"),
  ]);

  this.AB_data = []
  this.CD_Data = []

  this.AB_data = AB_data
  this.CD_Data = CD_Data

  ProgramScorecard.push({
    programId: p.programId, 
    programName: p.programName,
    AB_ViewModel: this.AB_data,
    CD_ViewModel: this.CD_Data,                     
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM