简体   繁体   中英

Returning a string based off of an objects key value pairs

I currently receive an object from a payload that I'm looking to format essentially into a string based off the values inside that object. I have a solution but I'm not really a big fan of it and know there is likely a more practical way of solving this.

the object that comes back in the payload looks like this

{
  sundayUsage: false,
  mondayUsage: true,
  tuesdayUsage: false,
  wednesdayUsage: true,
  thursdayUsage: true,
  fridayUsage: true,
  saturdayUsage: false
}

Based off of these values I want to be able to display a different string.

Here is my current solution

function formatSchedule(schedule) {
  let days = []
  let convertedSchedule = Object.keys(schedule).map(x => ({
    available: schedule[x],
    label: x.slice(0, 3).replace(/^[^ ]/g, match => match.toUpperCase())
  }))
  convertedSchedule.map(x => {
    if (x.available) {
      days.push(x.label)
    }
    return days
  })
  if (
    days.includes('Mon' && 'Tue' && 'Wed' && 'Thu' && 'Fri' && 'Sat' && 'Sun')
  ) {
    return 'Everyday'
  } else if (
    days.includes('Mon' && 'Tue' && 'Wed' && 'Thu' && 'Fri') &&
    !days.includes('Sat' && 'Sun')
  ) {
    return 'Weekdays (Mon-Fri)'
  } else if (
    days.includes('Sat' && 'Sun') &&
    !days.includes('Mon' && 'Tue' && 'Wed' && 'Thu' && 'Fri')
  ) {
    return 'Weekends (Sat-Sun)'
  } else return days.join(', ')
}

I don't feel like bringing in an external library is the necessary, but I am open to looking into suggestions if it ends up being the right tool for the job.

Slightly different approach, using Boolean as the function passed to some/every:

function formatSchedule(schedule) {
    const days = Object.values(schedule);
    const weekdays = days.slice(0, 5);
    const weekend = days.slice(-2);

    if (days.every(Boolean)) return 'Everyday';
    else if (weekdays.every(Boolean) && !weekend.some(Boolean)) return 'Weekdays (Mon-Fri)'
    else if (weekend.every(Boolean) && !weekdays.some(Boolean)) return 'Weekends (Sat-Sun)'
    else return Object.entries(schedule)
        .filter(v => v[1])
        .map(v => v[0][0].toUpperCase() + v[0].slice(1, 3))
        .join(', ');
}

I'd do (big parts can be reused):

  const days = ["monday", "tuesday", "wednesday", ,"thursday", "friday", "saturday", "sunday"];
  const weekdays = days.slice(0, 5);
  const weekend = days.slice(5);

  const every = (a, b) => a.every(it => b.includes(it));
  const some = (a, b) => a.some(it => b.includes(it));

  const capitalize = s => s[0].toUpperCase() + s.slice(1);

  const available = days.filter(day => obj[day + "Usage"]);

  if(available.length === days.length)
    return "Everyday";

  if(every(weekdays, available) && !some(weekend, available))
    return "Weekday";

  if(every(weekend, available) && !some(weekdays, available))
    return "Weekend";

  return available.map(it => capitalize(it.slice(0, 3))).join(", ");

Note that this

days.includes('Sat' && 'Sun')

will evaluate the && first and result in the last value (if all values before were truthy, all non empty strings are truthy), so its the same as:

 days.includes('Sun')

and thats obviously not what you want.

Given that your object just contains booleans as values, why not just use that for the conditions ?

function formatSchedule(schedule){
if(schedule.monday&&schedule.tuesday&&schedule.wednesday&&schedule.thursday&&schedule.friday&&schedule.saturday&&schedule.sunday)
  return "Everyday"
if((schedule.monday&&schedule.tuesday&&schedule.wednesday&&schedule.thursday&&schedule.friday)&&!(schedule.saturday||schedule.sunday))
  return "Weekdays"
if(!(schedule.monday||schedule.tuesday||schedule.wednesday||schedule.thursday&&schedule.friday)&&(schedule.saturday&&schedule.sunday))
  return "Weekends"
// Get keys here and return days if other coditions aren't matched here.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM