从后端以不同类型的变量获取数据

问题描述 投票:0回答:1

我想将来自后端的数据存储在scores类型变量中。

我将来自后端的数据存储为scores1

(5) [Array(4), Array(4), Array(4), Array(4), Array(4)]
0: (4) ["fake algorithm 1", "fake_scalar_1", "none", 0.679]
1: (4) ["csic's algorithm", "fake_time_series_1", "mean", 0.954]
2: (4) ["csic's algorithm", "step_length_right", "mean", 0.654]
3: (4) ["csic's algorithm", "step_length_left", "mean", 0.351]
4: (4) ["csic's algorithm", "step_time_right", "mean", 0.378]

我首先要放入分数对象。

Components.ts:

  experiment1: scores[] = [];
  id1: number;
  id2: number;
  selectedExperiments: number[];
  scores1: any;
  
  ngOnInit() {
      this.selectedExperiments = this.experimentService.getTheId();
      console.log(this.selectedExperiments);
      this.id1 = this.selectedExperiments[0];
      this.id2 = this.selectedExperiments[1];
      this.compareService.getScore(this.id1)
      .subscribe(response => {
        this.scores1 = response;
        for(var i = 0; i<(this.scores1.length-1);i++){
            this.experiment1[i].algorithm_name=this.scores1[i][0];
            this.experiment1[i].pi_name=this.scores1[i][1];
            this.experiment1[i].agg_type=this.scores1[i][2];
            this.experiment1[i].score=this.scores1[i][3];
            console.log(this.experiment1[i]);
        }
      }
    );
    this.compareService.getScore(this.id2)
    .subscribe(response => {
      this.scores2 = response;
      console.log(this.scores2)}
    );

  }

[分数等级:

export class scores {
  public algorithm_name: string;
  public pi_name: string;
  public agg_type: string;
  public score: number;
}

这是我得到的解决方案中最接近的解决方案,现在出现错误,说它不知道算法名称...该服务中的getScore()方法可以正常工作,使用console.log()我可以获得上面的数据。

angular typescript
1个回答
1
投票

您必须先填充this.experiment1,然后才能通过via访问值

this.experment1 [i] .whatevetPropertyName;

因此您可以:

for(var i = 0; i<(this.scores1.length-1);i++){
    const item = new scores();
    item.algorithm_name=this.scores1[i][0];
    item.pi_name=this.scores1[i][1];
    item.agg_type=this.scores1[i][2];
    item.score=this.scores1[i][3];
    this.experiment1.push(item);
}

类似上面的内容。在循环中创建并填充一个scores对象,然后将其推入数组。

© www.soinside.com 2019 - 2024. All rights reserved.