简体   繁体   English

如何从对象 Javascript 数组中检索花费的最高时间

[英]How to retrive highest time spent from array of object Javascript

I have an array of object in which I have calculated sessions(time spent) of particular ip .我有一个对象数组,我在其中计算了特定 ip 的会话(花费的时间) I have calculated spent time in hh:mm:ss format so that it will easy to understand.我以 hh:mm:ss 格式计算了花费的时间,以便于理解。

[ { start: 'Thu, 10 Jan 2019 06:04:49 GMT',
    end: 'Thu, 10 Jan 2019 06:12:22 GMT',
    total_spend: '00:07:27' },
  { start: 'Thu, 10 Jan 2019 07:58:18 GMT',
    end: 'Thu, 10 Jan 2019 07:59:09 GMT',
    total_spend: '00:00:50' },
  { start: 'Thu, 10 Jan 2019 09:28:00 GMT',
    end: 'Thu, 10 Jan 2019 09:59:46 GMT',
    total_spend: '00:31:46' } ]

now I want the highest of total_spend from above array ie现在我想要上面数组中最高的 total_spend

maximum time spent compare to all object that should be '00:31:46' (31 minutes, 46 seconds)与所有应为“00:31:46”(31 分 46 秒)的对象相比,花费的最长时间

I can't understand how I'll compare this as all comparison in this format so far I've seen was converted to date then compared.我无法理解我将如何进行比较,因为到目前为止我看到的所有这种格式的比较都转换为日期然后进行比较。 Either I can convert it into ms and then I should compare.要么我可以将它转换为 ms 然后我应该比较。 Is there any other way to execute this?有没有其他方法可以执行此操作?

Can you not just compare the strings?你不能只比较字符串吗? If you do 'stringA' < 'stringB', JavaScript will compare the two strings as if they were to be sorted alphabetically, based on unicode values, so a lower time will be correctly evaluated as "less than" a higher time.如果您执行 'stringA' < 'stringB',JavaScript 将比较两个字符串,就好像它们要根据 unicode 值按字母顺序排序一样,因此较低的时间将被正确评估为“小于”较高的时间。

Like so:像这样:

const sessions = [ 
  { start: 'Thu, 10 Jan 2019 06:04:49 GMT',
    end: 'Thu, 10 Jan 2019 06:12:22 GMT',
    total_spend: '00:07:27' },
  { start: 'Thu, 10 Jan 2019 07:58:18 GMT',
    end: 'Thu, 10 Jan 2019 07:59:09 GMT',
    total_spend: '00:00:50' },
  { start: 'Thu, 10 Jan 2019 09:28:00 GMT',
    end: 'Thu, 10 Jan 2019 09:59:46 GMT',
    total_spend: '00:31:46' } 
]

let highest = '00:00:00';
for (let session of sessions) {
  if (session.total_spend > highest) {
    highest = session.total_spend
  }
}
console.log(highest)

You could sort descending sessions array by taking difference from start and end properties and then just access the first element.您可以通过从startend属性中获取差异来sort降序会话数组进行sort ,然后只访问第一个元素。

 let sessions = [ { start: 'Thu, 10 Jan 2019 06:04:49 GMT', end: 'Thu, 10 Jan 2019 06:12:22 GMT', total_spend: '00:07:27' }, { start: 'Thu, 10 Jan 2019 07:58:18 GMT', end: 'Thu, 10 Jan 2019 07:59:09 GMT', total_spend: '00:00:50' }, { start: 'Thu, 10 Jan 2019 09:28:00 GMT', end: 'Thu, 10 Jan 2019 09:59:46 GMT', total_spend: '00:31:46' } ] sessions.sort((a,b) => (new Date(b.end) - new Date(b.start)) - (new Date(a.end) - new Date(a.start))); console.log(sessions[0].total_spend);

Another approach could be using reduce method.另一种方法是使用reduce方法。

 let sessions = [ { start: 'Thu, 10 Jan 2019 06:04:49 GMT', end: 'Thu, 10 Jan 2019 06:12:22 GMT', total_spend: '00:07:27' }, { start: 'Thu, 10 Jan 2019 07:58:18 GMT', end: 'Thu, 10 Jan 2019 07:59:09 GMT', total_spend: '00:00:50' }, { start: 'Thu, 10 Jan 2019 09:28:00 GMT', end: 'Thu, 10 Jan 2019 09:59:46 GMT', total_spend: '00:31:46' } ] let index = sessions.reduce((iMax, x, i, arr) => (new Date(x.end) - new Date(x.start)) > new Date(arr[iMax].end) - new Date(arr[iMax].start) ? i : iMax, 0); console.log(sessions[index].total_spend);

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

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