1

Is there a way to merge these two object so I can get the desired output(last code snippet)? I have an object generated_calendar that has Q1-Q4 with Periods 1-12 and the weeks object within the period.

var generated_calendar = {
    Q1 : {
        P1 : {
            WK1 : {
                start: '',
                end: ''
            },
            WK2 : {
                start: '',
                end: ''
            },
            WK3 : {
                start: '',
                end: ''
            }
        },
        P2 : {
            WK4 : {
                start: '',
                end: ''
            },
            WK5 : {
                start: '',
                end: ''
            },
            WK6 : {
                start: '',
                end: ''
            }
        },
        P3 : {
            WK7 : {
                start: '',
                end: ''
            },
            WK8 : {
                start: '',
                end: ''
            },
            WK9 : {
                start: '',
                end: ''
            }
        },
        P4 : {
            WK10 : {
                start: '',
                end: ''
            },
            WK11 : {
                start: '',
                end: ''
            },
            WK12 : {
                start: '',
                end: ''
            },
            WK13 : {
                start: '',
                end: ''
            }
        }
    },
    Q2 : {
        P5 : {
            WK14 : {
                start: '',
                end: ''
            }
        }
    }
}

and

var weeks = {
WK1 : {
    start: '1/1/2018',
    end: '1/7/2018'
},
WK2 : {
    start: '1/8/2018',
    end: '1/14/2018'
},
WK3 : {
    start: '1/15/2018',
    end: '1/21/2018'
}}

etc...to generate the following

var generated_calendar = {
    Q1 : {
        P1 : {
            WK1 : {
                start: '1/1/2018',
                end: '1/7/2018'
            },
            WK2 : {
                start: '1/8/2018',
                end: '1/14/2018'
            },
            WK3 : {
                start: '1/15/2018',
                end: '1/21/2018'
            },
        P2 : {
            WK4 : {
                start: '',
                end: ''
            }
         }
       }
    }

you get the idea, i'll have an object of 52 weeks and I want to merge it to the generated_calendar object that has the WK keys.

Marvine Chi
  • 177
  • 1
  • 3
  • 10

1 Answers1

0

Object.keys(generated_calendar).forEach( (q) => {
  Object.keys(generated_calendar[q]).forEach((p) => {
    Object.keys(generated_calendar[q][p]).forEach((w) => {
      if(weeks[w]){
        generated_calendar[q][p][w] = weeks[w]
      }
    });
  });
});
console.log(generated_calendar);