#Array of objects

2 messages · Page 1 of 1 (latest)

tall kiln
#

Hello! I'm struggling to get my array data correct.
I have this function that gets number of months from dates.
My question is, how could I add all of the data into single array. When i'm calling this function 4 times?

    public period(startDate: Date, endDate: Date, key: string) {
        const monthArrayObject = [
            {m: [], plan: [], year: ''},{m: [], plan: [], year: ''},{m: [], plan: [], year: ''},{m: [], plan: [], year: ''},
            {m: [], plan: [], year: ''},{m: [], plan: [], year: ''},{m: [], plan: [], year: ''},
            {m: [], plan: [], year: ''},
            {m: [], plan: [], year: ''},
            {m: [], plan: [], year: ''},
            {m: [], plan: [], year: ''},
            {m: [], plan: [], year: ''},
        ];

        const firstMonth = startDate.getMonth();
        const secondMonth = endDate.getMonth();
        let numberOfMonths = [];

        for (let month = firstMonth; month <= secondMonth; month++) {
            numberOfMonths.splice(month, 0, {month, key});
        }
        numberOfMonths.forEach(key => {
            monthArrayObject[key.month].m.push(key.month)
            monthArrayObject[key.month].plan.push(key.key)
        })
        // this returns the picture array but for each constant i have in combinePeriods()
        console.log('month', monthArrayObject)

        return numberOfMonths;
    }
    public combinePeriods() {
                const designProcurement = this.period(row.designProcurementStartDate, row.designProcurementEndDate, 'design-procurement');
                const designWork = this.period(row.designWorkStartDate, row.designWorkEndDate, 'design-work');
                const constructionProcurement = this.period(row.constructionProcurementStartDate, row.constructionProcurementEndDate, 'construction-procurement');
                const constructionWork = this.period(row.constructionWorkStartDate, row.constructionWorkEndDate, 'construction-work');
}
#

Hmm.. instead of using "return numberOfMonths" I got it working...