I am working with an array of Models where each object contains another array of Models. My goal is to calculate the sum of all the number variables from the nested arrays using the code snippet below.
Model
TimesheetLogged.ts
export interface TimesheetLogged {
ProjectId: string,
MondayHours: number,
TuesdayHours: number,
WednesdayHours: number,
ThursdayHours: number,
FridayHours: number,
SaturdayHours: number,
SundayHours: number
}
Project.ts
import { TimesheetLogged } from "./TimesheetLogged";
export interface Project {
ProjectId: number;
TimeLoggedHours: TimesheetLogged[];
}
Piece of code from Component
public Projects: Project[];
//Get projects data from database and subscribe to Projects object Successfully
let chartData: Array<number> = [];
let mon:number= 0;
let tue:number= 0;
let wed:number= 0;
let thu:number= 0;
let fri:number= 0;
let sat:number= 0;
let sun:number= 0;
this.Projects.forEach((empHours) => {
empHours.TimeLoggedHours.forEach((hours) => {
a => {
mon += a.MondayHours;
tue += a.TuesdayHours;
wed += a.WednesdayHours;
thu += a.ThursdayHours;
fri += a.FridayHours;
sat += a.SaturdayHours;
sun += a.SundayHours;
}
});
});
chartData.push(mon);
chartData.push(tue);
chartData.push(wed);
chartData.push(thu);
chartData.push(fri);
chartData.push(sat);
chartData.push(sun);
However I am getting Sum of all number variables as
[object Array][0, 0, 0, 0, 0, 0, 0]
Any update required in this code. Any other simpler solution is welcome.