如何在混合字符串和数字javascript中获得两次并比较它们

我得到以下格式的时间


我应该如何比较这两个并找到更大的一个?我的目标是对它们进行排序:


function (a, b) {

  //here is should find a way to extract a and b to find the right number to compare

  a = '16 days, 23 hours, 39 minutes';

  b = '39 minutes';

  return ((a < b) ? -1 : ((a > b) ? 1 : 0));

}


莫回无
浏览 163回答 3
3回答

FFIVE

使用正则表达式,我们可以先提取总时间并在方法中以分钟为单位进行转换getTimeInMinutes,然后相应地进行比较。像这样:function(a, b) {&nbsp; &nbsp;function getTimeInMinutes(timestamp) {&nbsp; &nbsp; &nbsp; const regex = /(?:(\d*?) days, )?(?:(\d*?) hours, )?(?:(\d*?) minutes)?/;&nbsp; &nbsp; &nbsp; let [, days = 0, hours = 0, minutes = 0] = regex.exec(timestamp);&nbsp; &nbsp; &nbsp; days = parseInt(days, 10);&nbsp; &nbsp; &nbsp; hours = parseInt(hours, 10);&nbsp; &nbsp; &nbsp; minutes = parseInt(minutes, 10);&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; const totalMinutes = (days * 24 * 60) + (hours * 60) + minutes;&nbsp; &nbsp; &nbsp; return totalMinutes;&nbsp; &nbsp;}&nbsp; &nbsp;const totalMinutesInA = getTimeInMinutes(a);&nbsp; &nbsp;const totalMinutesInB = getTimeInMinutes(b);&nbsp; &nbsp;&nbsp; &nbsp;return totalMinutesInA - totalMinutesInB;&nbsp; &nbsp;}function compare(a, b) {&nbsp; &nbsp;function getTimeInMinutes(timestamp) {&nbsp; &nbsp; &nbsp; const regex = /(?:(\d*?) days, )?(?:(\d*?) hours, )?(?:(\d*?) minutes)?/;&nbsp; &nbsp; &nbsp; let [, days = 0, hours = 0, minutes = 0] = regex.exec(timestamp);&nbsp; &nbsp; &nbsp; days = parseInt(days, 10);&nbsp; &nbsp; &nbsp; hours = parseInt(hours, 10);&nbsp; &nbsp; &nbsp; minutes = parseInt(minutes, 10);&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; const totalMinutes = (days * 24 * 60) + (hours * 60) + minutes;&nbsp; &nbsp; &nbsp; return totalMinutes;&nbsp; &nbsp;}&nbsp; &nbsp;const totalMinutesInA = getTimeInMinutes(a);&nbsp; &nbsp;const totalMinutesInB = getTimeInMinutes(b);&nbsp; &nbsp;&nbsp; &nbsp;console.log({totalMinutesInA, totalMinutesInB})&nbsp; &nbsp;&nbsp; &nbsp;return totalMinutesInA - totalMinutesInB;}console.log('Test 1', compare('4 minutes', '3 minutes'));console.log('Test 2', compare('24 minutes', '1 hours, 3 minutes'));console.log('Test 2', compare('2 days, 24 minutes', '2 days, 24 minutes'))

jeck猫

另一个相当冗长的选项是这样的:&nbsp;https ://jsfiddle.net/0k9asxeb/var a = '16 days, 23 hours, 39 minutes';var b = '39 minutes';const getDayMinutes = (string) => {&nbsp; if (!string.includes(' days')) {&nbsp; &nbsp; return 0;&nbsp; }&nbsp; const days = string.split(' days')[0];&nbsp; console.log(days);&nbsp; return days * 24 * 60;}const getHoursMinutes = (string) => {&nbsp; if (!string.includes (' hours')) {&nbsp; &nbsp; return 0;&nbsp; }&nbsp; const hoursDirty = string.split(' hours')[0];&nbsp; console.log(hoursDirty);&nbsp; if (!hoursDirty.includes(',')) {&nbsp; &nbsp; return hoursDirty;&nbsp; }&nbsp; const hours = hoursDirty.slice(hoursDirty.lastIndexOf(',') + 2, hoursDirty.length);&nbsp; console.log(hours);&nbsp; return hours * 60;}const getMinutes = (string) => {&nbsp; if (!string.includes(' minutes')) {&nbsp; &nbsp; return 0;&nbsp; }&nbsp; const minutesDirty = string.split(' minutes')[0];&nbsp; console.log(minutesDirty);&nbsp; if (!minutesDirty.includes(',')) {&nbsp; &nbsp; return minutesDirty;&nbsp; }&nbsp; const minutes = minutesDirty.slice(minutesDirty.lastIndexOf(',') + 2, minutesDirty.length);&nbsp; console.log(minutes);&nbsp; return minutes;}const getTotalMinutesInString = (string) => {&nbsp; const dayMinutes = getDayMinutes(string);&nbsp; console.log('dayMinutes', dayMinutes);&nbsp; const hoursMinutes = getHoursMinutes(string);&nbsp; console.log('hoursMinutes', hoursMinutes);&nbsp; const minutes = getMinutes(string);&nbsp; console.log('minutes', minutes);&nbsp; return parseInt(dayMinutes + hoursMinutes + minutes);}const totalMinutes = getTotalMinutesInString(b);console.log(totalMinutes);

桃花长相依

创建一个将字符串转换为秒的函数。这是一个例子:const toTimeConvertor = (timeEntry) => {&nbsp; &nbsp; const string = timeEntry.replace(',', '');&nbsp; &nbsp; switch(string) {&nbsp; &nbsp; case 'day':&nbsp; &nbsp; case 'days':&nbsp; &nbsp; &nbsp; &nbsp; return 60 * 60 * 24;&nbsp; &nbsp; case 'hour':&nbsp; &nbsp; case 'hours':&nbsp; &nbsp; &nbsp; &nbsp; return 60 * 60;&nbsp; &nbsp; case 'minute':&nbsp; &nbsp; case 'minutes':&nbsp; &nbsp; &nbsp; &nbsp; return 60;&nbsp; &nbsp; case 'second':&nbsp; &nbsp; case 'seconds':&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; }};const stringToSeconds = (timeString) => {&nbsp; &nbsp; return [...timeString.split(',')].reduce((seconds, entry) => {&nbsp; &nbsp; const values = entry.trim().split(' ');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; const timeNumberString = values[0];&nbsp; &nbsp; const timeConvertorString = values[1];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; const timeNumber = Number.parseInt(timeNumberString);&nbsp; &nbsp; const timeConvertor = toTimeConvertor(timeConvertorString)&nbsp; &nbsp; const time = timeNumber * timeConvertor;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return seconds + time;&nbsp; }, 0);};const myFilter = (a, b) => stringToSeconds(a) - stringToSeconds(b);//Demo Outputconst list = [&nbsp; &nbsp; '16 days, 23 hours, 39 minutes',&nbsp; &nbsp; '39 minutes',&nbsp; '5 days, 36 minutes'];const sortedList = list.sort(myFilter);console.log(list);注意:如果输入是由用户生成的,您可能希望包括更多的生产检查。祝你好运。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript