Saya memiliki objek dengan nama hari dalam seminggu sebagai kunci yang nilainya adalah array objek dengan informasi tentang waktu buka dan tutup dalam shift yang berbeda.

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' }
    ],
};

Saya mencoba membuat perubahan kecil pada data JSON. Untuk nilai nama hari, saya mencoba menambahkan objek yang kuncinya adalah shift dan nilainya adalah array waktu dari waktu mulai hingga waktu penutupan dengan selisih 15 menit di antaranya.

enter image description here

Meskipun saya berhasil membuat daftar, saya tidak dapat mengetahui mengapa saya mendapatkan nilai yang persis sama sepanjang hari. Saya kehilangan sesuatu yang sederhana di suatu tempat.

enter image description here

Berikut kode saya.

let Object1 = {};
let Object2 = {};

for (let key in openingTimes)
{
    openingTimes[key].forEach(item =>
    {
         /* Separate opening time to parts by colon */
        let oParts = item.opening_time.split(':');
        let oHour = oParts[0];
        let oMins = oParts[1];

        /* Separate closing time to parts by 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 and return the 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);
    for (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;
}
2
anonym 15 Agustus 2017, 05:32

2 jawaban

Jawaban Terbaik

Objek dilewatkan dengan referensi dalam JavaScript, jadi Anda secara efektif menambahkan objek yang sama setiap kali.

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 = {};

for (let key in openingTimes) {

  let Object1 = {};

  openingTimes[key].forEach(item => {
    /* Separate opening time to parts by colon */
    let oParts = item.opening_time.split(':');
    let oHour = oParts[0];
    let oMins = oParts[1];

    /* Separate closing time to parts by 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 and return the 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);
  for (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;
}
0
K Scandrett 15 Agustus 2017, 02:53

Saya mengambil pendekatan yang lebih fungsional.

Ketika Anda memiliki daftar data yang perlu Anda ubah ke daftar data lain, saya berpendapat bahwa map, filter, dan reduce adalah cara yang tepat.

Ini adalah video yang layak yang menjelaskan idenya

Semoga ini menarik.

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'
    }
  ],
};

const fifteenMinutes = new Date('1970-01-01 00:15:00') - new Date('1970-01-01 00:00:00');

// notice the single assignment and no for-loops
const transformedOpeningTimes = (Object
  // grab the keys
  .keys(openingTimes)
  // map to tuples of key values
  .map(dayOfTheWeek => ({
    dayOfTheWeek,
    times: openingTimes[dayOfTheWeek]
  }))
  // map the values to `shifts`
  .map(({
    dayOfTheWeek,
    times
  }) => ({
    dayOfTheWeek,
    // for each time, reduce into the `shifts` object
    shifts: times.reduce((shifts, time) => {
      const {
        opening_time,
        closing_time,
        shift
      } = time;
      const lower = new Date(`1970-01-01 ${opening_time}`).getTime();
      const upper = new Date(`1970-01-01 ${closing_time}`).getTime();
      // calculate how many interval we need
      const intervals = parseInt((upper - lower) / fifteenMinutes);
      shifts[shift] = (Array(intervals)
        // create an array with the same length of intervals
        .fill(0)
        // map them to dates
        .map((_, index) => new Date(index * fifteenMinutes + lower))
        // map the dates to time strings
        .map(date => date.toTimeString())
        // convert the time strings to other time strings
        .map(dateString => {
          const hours = parseInt(dateString.slice(0, 2));
          const amOrPm = hours > 12 ? 'AM' : 'PM';
          return `${hours % 12}:${dateString.slice(3, 5)} ${amOrPm}`;
        })
      );
      return shifts;
    }, {})
  }))
  // reduce back into the object
  .reduce((transformedOpeningTimes, {
    dayOfTheWeek,
    shifts
  }) => {
    transformedOpeningTimes[dayOfTheWeek] = shifts;
    return transformedOpeningTimes;
  }, {})
);

console.log(transformedOpeningTimes);
1
Rico Kahler 15 Agustus 2017, 04:26