【问题标题】:Get data from the backend in a different type variable从后端获取不同类型变量中的数据
【发布时间】:2019-11-22 10:42:23
【问题描述】:

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

我将后端的数据存储为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() 我得到了上面的数据。

【问题讨论】:

  • 你能不能 console.log(this.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]
  • 好的,但我认为我的答案应该可以解决您的问题,只要您的 this.scores1 具有正确的响应值并且在访问 this.scores1[i][blah] 时也可以为您提供正确的值

标签: angular typescript


【解决方案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);
}

类似上面的东西。在循环中创建并填充分数对象,然后将其推送到数组中。

【讨论】:

    猜你喜欢
    • 2018-10-04
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2018-07-30
    相关资源
    最近更新 更多