2017-01-08 4 views
0

にオブジェクトのプロパティを1つ取得し、私は次のオブジェクトは角 - 配列

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に取得するにはどうすればよいですか?

答えて

2

使用Array.prototype.map

this.hoursArray = source.map(obj => obj.hours); 

またそれができる:

private hoursArray: Array<string>; 

それとも単に:

private hoursArray: string[]; 
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); 
     } 
    }); 
} 
関連する問題