简体   繁体   中英

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 . I have calculated spent time in hh:mm:ss format so that it will easy to understand.

[ { 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

maximum time spent compare to all object that should be '00:31:46' (31 minutes, 46 seconds)

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

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.

 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.

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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