0

I have an object which looks as so:

const hours = {
   Monday: ['9:00 am', '4:00 pm'],
   Tuesday: ['9:00 am', '4:00 pm'],
   Wednesday:['9:00 am', '3:00 pm']
   Thursday: ['9:00 am', '3:00 pm'],
   Friday: ['9:00 am', '3:00 pm'],
   Saturday: "Closed",
   Sunday: "Closed",
}

I then get the current day of the week in a string format:

  const d = new Date();
  let day = d.getDay();

  const daysOfWeek = {
    1: 'Monday',
    2: 'Tuesday',
    3: 'Wednesday',
    4: 'Thursday',
    5: 'Friday',
    6: 'Saturday',
    7: 'Sunday'
  }


  const currentDayString = daysOfWeek[day]

I now need to create a new object that has the current day, up until sunday.

ex output

if the day is 'Friday' the hours obj should be:


{
  Friday: ['9:00 am', '3:00 pm'],
  Saturday: "Closed",
  Sunday: "Closed"
}

How can I achieve this?

Ben_Sven_Ten
  • 529
  • 3
  • 21

6 Answers6

2

Let today's day be d (ex: 'Monday', 'Tuesday') then, and serverData be data we are getting from server so,

let found=0;
let result={}

Object.keys(daysOfWeek).forEach(key => {
  if (found) {
    result[daysOfWeek[key]] = serverData[daysOfWeek[key]]
  };
  if (daysOfWeek[key] === d) {
    found = 1;
  }
})
0

Let say your starting day is "thursday", your result object could never be like this :

{
  Thursday: (2) ['9:00 am', '3:00 pm'
  Friday: (2) ['9:00 am', '3:00 pm']
  Saturday: "Closed"
  Sunday: "Closed"
}

Cause JS orders an object alphabetically.

Try to push your results into an array like this (data been your server data):

let result = [];
for(let i=day; i <= Object.keys(daysOfWeek).length; i++)
{
     // result[daysOfWeek[i]] = data[daysOfWeek[i]]; // return object
     result.push(data[daysOfWeek[i]]); // return array
}
bycrea
  • 1
  • 1
0

If your input data always contain all the week days in the right order, you can simply use the object properties index to determine which day it is:

const data = {
  Monday: ['9:00 am', '4:00 pm'],
  Tuesday: ['9:00 am', '4:00 pm'],
  Wednesday: ['9:00 am', '3:00 pm'],
  Thursday: ['9:00 am', '3:00 pm'],
  Friday: ['9:00 am', '3:00 pm'],
  Saturday: 'Closed',
  Sunday: 'Closed',
};

const today = new Date().getDay();

const result = {};
Object.entries(data).forEach(([key, value], index) => {
  if (index >= today - 1) result[key] = value;
});

console.log(result);
Ben
  • 1,331
  • 2
  • 8
  • 15
0

Can be done with reduce

const data = {
  Monday: ['9:00 am', '4:00 pm'],
  Tuesday: ['9:00 am', '4:00 pm'],
  Wednesday: ['9:00 am', '3:00 pm'],
  Thursday: ['9:00 am', '3:00 pm'],
  Friday: ['9:00 am', '3:00 pm'],
  Saturday: 'Closed',
  Sunday: 'Closed',
};

const day = new Date().toLocaleString('en', { weekday: 'long' });

const result = Object.entries(data).reduce((prev, [key, value]) => {
  if (key === day || day in prev) {
    prev[key] = value;
  }
  
  return prev;
}, {});

console.log(result);
Joel
  • 1,187
  • 1
  • 6
  • 15
0

The easiest way of doing it would be to step through some dates, starting with the current date dt. Show the opening times for the current day and then increment the date by one day. Repeat this process as long as wd!== "Sunday".

const opening={ // exactly as received from the server:
Friday:   ['9:00 am', '3:00 pm'],
Monday:   ['9:00 am', '4:00 pm'],
Saturday: "Closed",
Sunday:   "Closed",
Thursday: ['9:00 am', '3:00 pm'],
Tuesday:  ['9:00 am', '4:00 pm'],
Wednesday:['9:00 am', '3:00 pm']};

const dt=new Date();
do {
 var wd=dt.toLocaleString("en",{weekday:"long"});
 console.log(`${wd}: ${opening[wd]}`);
 dt.setDate(dt.getDate()+1);
} while(wd!=="Sunday") // stop after "Sunday"

Doing it this way we don't require any "order" of the properties in the opening object. Keep in mind that you cannot always rely on the order of properties in an object anyway!

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

My solution was to use a daysOfWeek object to bridge the data between hours and day by storing the keys of hours as the values of daysOfWeek and the keys of daysOfWeek to match any of the possible values of .getDay() (1-7 for each day of the week).

Works well for THIS particular use case.

const hours = {
   Monday: ['9:00 am', '4:00 pm'],
   Tuesday: ['9:00 am', '4:00 pm'],
   Wednesday: ['9:00 am', '3:00 pm'],
   Thursday: ['9:00 am', '3:00 pm'],
   Friday: ['9:00 am', '3:00 pm'],
   Saturday: "Closed",
   Sunday: "Closed"
};

const d = new Date();
let day = d.getDay(); // if Friday, day === 5


const daysOfWeek = [ 
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday',
  'Saturday',
  'Sunday'
];

let thisWeekHours = {};

for (let i = day - 1; i < 7; i++) { // i === 5, 6, 7 (day value will never exceed 7), -1 because the array start at 0 and date at 1
  const hoursKey = daysOfWeek[i] // hoursKey === 'Friday', 'Saturday', 'Sunday'
  thisWeekHours[hoursKey] = hours[hoursKey] // Setting property name to hoursKey, && value to hours at key of hoursKey
}

console.log(thisWeekHours);

For example, output for friday:

thisWeekHours = {
   'Friday': ['9:00', '3:00'],
   'Saturday': 'Closed',
   'Sunday': 'Closed'
}
Elikill58
  • 4,050
  • 24
  • 23
  • 45
Ben_Sven_Ten
  • 529
  • 3
  • 21