我有以下对象
export class HourPerMonth {
constructor(
public year_month: string,
public hours: string,
public amount: string
) { };
}
现在我想用一个只有几小时来填充一个数组。
private hourPerMonth: HourPerMonth[];
private hoursArray: Array<any>;
getChartData() {
this.chartService.getHoursPerMonth().subscribe(source => {
this.hourPerMonth = source;
this.hoursArray = ?
});
}
如何将对象的小时数输入hoursArray?
答案 0 :(得分:2)
使用https://youtu.be/zQt7GGkR7As?t=5m31s:
?>
也可以是:
this.hoursArray = source.map(obj => obj.hours);
或者简单地说:
private hoursArray: Array<string>;
答案 1 :(得分:0)
这种方式适合你。
private hourPerMonth: HourPerMonth[];
private hoursArray: Array<any> = [];
getChartData() {
this.chartService.getHoursPerMonth().subscribe(source => {
this.hourPerMonth = source;
this.hourPerMonth.forEach(hourPerMonth => {
this.hoursArray.push(hourPerMonth.hours);
}
});
}