简体   繁体   English

Javascript天输出为范围格式?

[英]Javascript days output to range format?

I have a javascript object which contains the days of the week and a true/false value if they have been selected or not. 我有一个javascript对象,其中包含一周中的某几天以及是否选择了true/false值。

I am then passing this object to a function where I need to format the days in a specific way and return it. 然后,我将此对象传递给一个函数,在该函数中,我需要以一种特定的方式格式化日期并返回它。

Essentially, if any days follow another day that is also true, it will display those selected days as a range such as MF . 本质上,如果在第二天也是如此,那么它将显示那些选定的日期,例如MF Where as if the days selected were M, T, F then the output would be MT, F 假设所选的日期是M, T, F则输出将是MT, F

Here is an example of how I am trying to set this up: 这是我尝试设置此方法的示例:

// Define our selected days
var days = {
  sunday: true,
  monday: false,
  tuesday: false,
  wednesday: true,
  thursday: true,
  friday: false,
  saturday: false
};

// Format our dates
function formatDates(days) {
  // Logic Here
  return days;
}

console.log(formatDates(days));

// Scenarios 

/* Input 
1: Checked: Su, W, Th
2: Checked: Su, M, T, Sa
3: Checked: M, T, Th, Sa
4: Checked: T, Th, Sa, Sun
/*

/*
Output
1: Su, W, Th
2: Sa - T
3: M - T, Th, Sa
4: Sa - Su, T, Th
/*

Here is a fiddle to the above code: https://jsfiddle.net/ghs3Lthj/1 这是上面代码的小提琴: https : //jsfiddle.net/ghs3Lthj/1

Question: Are there any date functions that I should be aware of that handle ranges like this or would it need to be custom? 问题:是否有任何我应该知道的日期函数可以处理这样的范围,或者是否需要自定义?

Just not sure if anything exists where I can say "shows me these days in a range-style format". 只是不确定是否存在任何可以说“这些天以范围样式显示给我看”的内容。

As properties in objects have no guaranteed order, you would better use an array for your days. 由于对象中的属性没有确定的顺序,因此最好在您的日子中使用数组。 Arrays are suitable when order is important. 当顺序很重要时,数组是合适的。

Also, you would need to define the abbreviations for each day. 另外,您将需要定义每天的缩写。

So I would suggest this structure: 所以我建议这种结构:

var days = [
  { name: "sunday",    abbrev: "Su", selected: true  },
  { name: "monday",    abbrev: "M",  selected: false },
  { name: "tuesday",   abbrev: "Tu", selected: false },
  { name: "wednesday", abbrev: "W",  selected: true  },
  { name: "thursday",  abbrev: "Th", selected: true  },
  { name: "friday",    abbrev: "F",  selected: false },
  { name: "saturday",  abbrev: "Sa", selected: false }
];

I would then create a simple comma-separated string of all selected days, and then use string replacements to get to the desired format: 然后,我将为所有选定的日期创建一个简单的逗号分隔的字符串,然后使用字符串替换来获得所需的格式:

 function formatDates(days) { return days.map( day => day.selected ? day.abbrev : "" ) .join(", ") .replace(/(\\w+)(?:, \\w+)*, (\\w+)/g, "$1 - $2") // insert hyphen were appropriate .replace(/^(, )+|, (?=,|$)/g, "") // remove commas we don't need } var days = [ { name: "sunday", abbrev: "Su", selected: true }, { name: "monday", abbrev: "M", selected: false }, { name: "tuesday", abbrev: "Tu", selected: false }, { name: "wednesday", abbrev: "W", selected: true }, { name: "thursday", abbrev: "Th", selected: true }, { name: "friday", abbrev: "F", selected: false }, { name: "saturday", abbrev: "Sa", selected: false } ]; console.log(formatDates(days)); 

NB: Pairs of consecutive days will also be displayed with a hyphen, even though a comma would do. 注意:即使使用逗号,连续两天也会显示连字符。 If you prefer the comma in that case, replace the * in the first regular expression with a + . 如果在这种情况下更喜欢逗号,请用+替换第一个正则表达式中的*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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