0

I have the following object

export class HourPerMonth {
    constructor(
        public year_month: string,
        public hours: string,
        public amount: string
    ) { };
}

Now i want to fill an array with only the hours from the object.

private hourPerMonth: HourPerMonth[];
private hoursArray: Array<any>;

getChartData() {
    this.chartService.getHoursPerMonth().subscribe(source => {
        this.hourPerMonth = source;
        this.hoursArray = ?
    });
}

How do i get the hours from the object into the hoursArray?

2 Answers 2

3

Use Array.prototype.map:

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

Also it can be:

private hoursArray: Array<string>;

Or simply:

private hoursArray: string[];
Sign up to request clarification or add additional context in comments.

Comments

0

This way should work for you.

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);
       }
   });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.