Why am I getting repeated values in the following JavaScript operation? -


i have object names of days in week keys values array of objects information opening , closing time in different shifts.

let openingtimes =  {     sunday: [         { id: 1, shift: 'morning', opening_time: '07:00', closing_time: '11:00' },         { id: 1, shift: 'day',    opening_time: '13:30', closing_time: '16:30' },         { id: 1, shift: 'night', opening_time: '20:00', closing_time: '23:00' }     ],     monday: [         { id: 1, shift: 'day',    opening_time: '13:30', closing_time: '16:30' }     ],     wednesday: [         { id: 1, shift: 'morning', opening_time: '07:00', closing_time: '12:00' },         { id: 1, shift: 'night', opening_time: '20:00', closing_time: '23:00' }     ],     thursday: [         { id: 1, shift: 'morning', opening_time: '07:00', closing_time: '12:00' },         { id: 1, shift: 'night', opening_time: '20:00', closing_time: '23:00' }     ],     friday: [         { id: 1, shift: 'day',    opening_time: '13:30', closing_time: '16:30' },         { id: 1, shift: 'night', opening_time: '20:00', closing_time: '23:00' }     ], }; 

i'm trying make small change in json data. values of name of days, i'm trying add objects keys shifts , values array of time starting time closing time difference of 15 minutes in between them.

enter image description here

while did succeed in building list, i'm unable figure out why i'm getting same values days. i'm missing simple somewhere.

enter image description here

here's code.

let object1 = {}; let object2 = {};  (let key in openingtimes) {     openingtimes[key].foreach(item =>     {          /* separate opening time parts colon */         let oparts = item.opening_time.split(':');         let ohour = oparts[0];         let omins = oparts[1];          /* separate closing time parts colon */         let cparts = item.closing_time.split(':');         let chour = cparts[0];         let cmins = cparts[1];          object1[item.shift] = buildtimearray(ohour, omins, chour, cmins, 15);          object2[key] = object1;     }); }  console.log(object2);  /**  * gather restaurant hours , return array of available delivery times in difference of specified minutes  * @params  opening hour, opening minutes, closing hour, closing minutes, miutes interval  */ function buildtimearray(ohour, omins, chour, cmins, interval_mins) {     let result = [];     let start = new date('','','',ohour, omins);     let end = new date('','','',chour, cmins);     (let d = start; d <= end; d.setminutes(d.getminutes() + interval_mins))     {         result.push(this.meridiantime(d));     }     return result; }  /**  * build time in 12 hrs am/pm format  */ function meridiantime(inputdate) {     let hours = inputdate.gethours();     let minutes = inputdate.getminutes();     let ampm = hours < 12 ? "am" : (hours = hours % 12, "pm");      hours =  hours === 0 ? 12 : hours < 10? ("0" + hours) : hours;     minutes = minutes < 10 ? ("0" + minutes) : minutes;     return hours + ":" + minutes + " " + ampm; } 

objects passed reference in javascript, adding same object each time.

let openingtimes = {    sunday: [{        id: 1,        shift: 'morning',        opening_time: '07:00',        closing_time: '11:00'      },      {        id: 1,        shift: 'day',        opening_time: '13:30',        closing_time: '16:30'      },      {        id: 1,        shift: 'night',        opening_time: '20:00',        closing_time: '23:00'      }    ],    monday: [{      id: 1,      shift: 'day',      opening_time: '13:30',      closing_time: '16:30'    }],    wednesday: [{        id: 1,        shift: 'morning',        opening_time: '07:00',        closing_time: '12:00'      },      {        id: 1,        shift: 'night',        opening_time: '20:00',        closing_time: '23:00'      }    ],    thursday: [{        id: 1,        shift: 'morning',        opening_time: '07:00',        closing_time: '12:00'      },      {        id: 1,        shift: 'night',        opening_time: '20:00',        closing_time: '23:00'      }    ],    friday: [{        id: 1,        shift: 'day',        opening_time: '13:30',        closing_time: '16:30'      },      {        id: 1,        shift: 'night',        opening_time: '20:00',        closing_time: '23:00'      }    ],  };    let object2 = {};    (let key in openingtimes) {      let object1 = {};      openingtimes[key].foreach(item => {      /* separate opening time parts colon */      let oparts = item.opening_time.split(':');      let ohour = oparts[0];      let omins = oparts[1];        /* separate closing time parts colon */      let cparts = item.closing_time.split(':');      let chour = cparts[0];      let cmins = cparts[1];        object1[item.shift] = buildtimearray(ohour, omins, chour, cmins, 15);        object2[key] = object1;    });  }  console.log(object2);      /**   * gather restaurant hours , return array of available delivery times in difference of specified minutes   * @params  opening hour, opening minutes, closing hour, closing minutes, miutes interval   */  function buildtimearray(ohour, omins, chour, cmins, interval_mins) {    let result = [];    let start = new date('', '', '', ohour, omins);    let end = new date('', '', '', chour, cmins);    (let d = start; d <= end; d.setminutes(d.getminutes() + interval_mins)) {      result.push(this.meridiantime(d));    }    return result;  }    /**   * build time in 12 hrs am/pm format   */  function meridiantime(inputdate) {    let hours = inputdate.gethours();    let minutes = inputdate.getminutes();    let ampm = hours < 12 ? "am" : (hours = hours % 12, "pm");      hours = hours === 0 ? 12 : hours < 10 ? ("0" + hours) : hours;    minutes = minutes < 10 ? ("0" + minutes) : minutes;    return hours + ":" + minutes + " " + ampm;  }


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -