iNeoO / vue-meeting-selector

This component is inspired from the meeting selector from doctolib with the power of Vuejs components.
https://vue-meeting-selector.tuturu.io
95 stars 19 forks source link

How Generate Slots From array of dates saved got from back-end #33

Closed AhmadAlaswadDev closed 2 years ago

AhmadAlaswadDev commented 2 years ago

Hi

How Generate Slots From array of dates saved got from back-end

xxxxx

iNeoO commented 2 years ago

Hi, i suppose you could do something like that

const dates = []; // dates send by api

const slots = dates.reduce((acc, elem) => {
  const d = new Date(elem.date);
  const date = `${d.getFullYear}-${d.getMonth() + 1}-${d.getDate()}`;
  const slotIndex = acc.findIndex((slot) => slot.date === date);
  if (slotIndex !== 1) {
    acc[slotIndex].slots.push(elem);
  } else {
    acc.push({
      date,
      slots: [elem],
    });
  }
  return acc;
}, []);