angular - 从后端获取不同类型变量的数据
问题描述
我想将来自后端的数据存储在分数类型变量中。
我将后端的数据存储为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]
我首先想放入我的分数对象。
组件.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()
我得到上面的数据。
解决方案
您的 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);
}
类似上面的东西。在循环中创建并填充分数对象,然后将其推送到数组中。
推荐阅读
- angular - 基于百分比的高图背景
- ruby-on-rails - Rails Bootstrap 导航栏显示问题
- php - 生产服务器上的 FPDF 字体错误但在本地服务器上工作
- javascript - 对象属性在在线代码编辑器中按字母顺序排序
- r - 以长格式展开嵌套列
- node.js - 如何设置 node-cron 在 12 小时内运行
- python - 关于 corr() 函数 python 中的 min_period
- reactjs - 将手风琴反应为全部展开和全部折叠
- jenkins - Jenkins JNLP Linux 代理 - Ping 超时
- msbuild - 您如何以编程方式找出 NuGet.exe 在给定计算机上的安装位置?