简体   繁体   English

JavaScript:如何使用switch语句显示时间?

[英]JavaScript: How to display time using a switch statement?

I want to generate a custom time-formatted string via a switch statement where the date information is passed in an array. 我想通过switch语句生成一个自定义的时间格式的字符串,在该语句中,日期信息在数组中传递。

For an array like this: 对于这样的数组:

let displaySumTimes = ['00', '00', '10']

the expected output would be: 预期的输出将是:

10sec

If a value in the array is greater than zero, then that should be included in the formatted string that is returned from my switch statement. 如果数组中的值大于零,则该值应包含在我的switch语句返回的格式化字符串中。 Each non-zero value in the result also needs to have the corresponding time unit included with it in the formatted string result. 结果中的每个非零值还需要在格式化的字符串结果中包含相应的时间单位。

My current code looks like this: 我当前的代码如下所示:

 let displaySumTimes = ['00', '00', '10'];

 const formatTime = (time) => {
  const [hour, minute, sec] = time.split(':');
  console.log([hour, minute, sec]);
  switch([hour, minute, sec]) {
    case hour > 0:
      return `${hour} h ${minute} min ${sec} sec`;
      break;
    case minute > 0:
      return `${minute} min ${sec} sec`;
      break;
    case minute < 1:
      return `${sec} sec`;
    default:
      // code block
  }  
}

 formatTime(displaySumTimes); //output 10sec

 let displaySumTimes1 = ['00', '10', '10'];


 formatTime(displaySumTimes1); //output 10min 10 sec

It looks like your switch syntax is incorrect. 看来您的切换语法不正确。 You are not destructuring there, you're making a switch on a new array (which you're creating there in the conditional). 您并没有在这里进行破坏,而是在新数组上进行了切换(您将在条件数组中创建该数组)。 Why not simply refactor your cases to be ifs? 为什么不简单地将案件重构为假设呢?

Also, you're using a string method ( split ) on an array. 另外,您在数组上使用字符串方法( split )。 I don't see any strings with ":"s in your code. 我在您的代码中看不到任何带有“:”的字符串。 If you're going to be using an array which you know will look like that ['##', '##', '##'] , you could use ifs like this: 如果您要使用的数组看起来像['##', '##', '##'] ,则可以使用ifs,如下所示:

if (hour !== "00") return `${hour} h ${minute} min ${sec} sec`;
if (minute !== "00") return `${minute} min ${sec} sec`;
return `${sec} sec`;

Perhaps you could take a functional approach to this which would avoid the need for a switch statement. 也许您可以采取一种实用的方法来避免使用switch语句。

For instance, you could dynamically build a list containing each part of the formatted string result (which is built based on the input values), and then join() that list a white-space character to compose a string with the required format: 例如,您可以动态构建一个包含格式化字符串结果各部分(基于输入值构建join()列表,然后join()列出一个空白字符以组成具有所需格式的字符串:

 const formatTime = (time) => { const [h, m, s] = time; /* Dynamically build a temporary list containing each part of the formatted time depending on input values supplied*/ return [].concat( h > 0 ? [`${ h }hr`] : [], m > 0 ? [`${ m }min`] : [], s > 0 ? [`${ s }sec`] : []) /* Join each part of the formatted time with a whitespace to achieve required formatting */ .join(' '); } let displaySumTimes = ['00', '00', '10']; let displaySumTimes1 = ['00', '10', '10']; console.log(formatTime(displaySumTimes)); //output 10sec console.log(formatTime(displaySumTimes1)); //output 10min 10 sec 

Interesting... 有趣...

In your function switch will compare reference on array [hour, minute, sec] with boolean values in each cases (hour > 0, minute > 0 and minute < 1). 在您的功能开关中,将在每种情况下(小时> 0,分钟> 0和分钟<1)将数组[hour,minutes,sec]上的引用与布尔值进行比较。 It is not working... 它不起作用...

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

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