根据对象键值对返回字符串

时间:2019-07-11 15:15:15

标签: javascript object formatting

我目前正在从有效载荷中接收一个对象,该对象实际上是根据该对象内部的值格式化为字符串。我有一个解决方案,但我不是很喜欢它,并且知道可能有更实际的解决方案。

返回有效载荷的对象看起来像这样

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

基于这些值,我希望能够显示其他字符串。

这是我当前的解决方案

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(', ')
}

我不认为必须引入外部库,但是如果它最终成为适合该工作的工具,我愿意寻求建议。

3 个答案:

答案 0 :(得分:2)

使用稍微不同的方法,将Boolean作为函数传递给某些/每个人:

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

答案 1 :(得分:1)

我愿意(大部分零件可以重复使用):

  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(", ");

请注意,

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

将首先评估&&并得出最后一个值(如果之前的所有值均为真,则所有非空字符串均为真),因此其与以下内容相同:

 days.includes('Sun')

那显然不是您想要的。

答案 2 :(得分:1)

鉴于您的对象仅包含布尔值,为什么不将其用作条件呢?

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.
}