Find the largest, smallest, and average value for an array of hashes with common keys?

user2974739

I have an array of hashes with the information below. I am trying to get the largest, smallest, and average for "timeAsleep". I have tried the answers from these questions, but it doesn't seem to work.

Find the largest value for an array of hashes with common keys?

Finding the element of a Ruby array with the maximum value for a particular attribute

rails select maximum value from array of hash

Here is the array of hashes

{"id"=>"1", "userId"=>"1", "day"=>"2015-12-05", "startTime"=>"2015-12-05T07:49:30.000Z", "endTime"=>"2015-12-05T15:56:30.000Z", "tzOffset"=>"-08:00", "source"=>"misfit", "mainSleep"=>true, "timeAsleep"=>478, "timeAwake"=>9, "efficiency"=>0, "timeToFallAsleep"=>0, "timeAfterWakeup"=>0, "numberOfWakeups"=>1, "timeInBed"=>0, "createdAt"=>"2015-12-05T21:59:24.935Z", "updatedAt"=>"2015-12-05T21:59:24.935Z", "humanId"=>"1"}
{"id"=>"2", "userId"=>"2", "day"=>"2015-12-04", "startTime"=>"2015-12-04T07:02:30.000Z", "endTime"=>"2015-12-04T14:59:30.000Z", "tzOffset"=>"-08:00", "source"=>"misfit", "mainSleep"=>true, "timeAsleep"=>471, "timeAwake"=>6, "efficiency"=>0, "timeToFallAsleep"=>0, "timeAfterWakeup"=>0, "numberOfWakeups"=>1, "timeInBed"=>0, "createdAt"=>"2015-12-04T16:41:44.198Z", "updatedAt"=>"2015-12-04T16:41:44.198Z", "humanId"=>"2"}
{"id"=>"3", "userId"=>"3", "day"=>"2015-12-03", "startTime"=>"2015-12-03T06:02:32.000Z", "endTime"=>"2015-12-03T13:38:32.000Z", "tzOffset"=>"-08:00", "source"=>"misfit", "mainSleep"=>true, "timeAsleep"=>434, "timeAwake"=>22, "efficiency"=>0, "timeToFallAsleep"=>0, "timeAfterWakeup"=>0, "numberOfWakeups"=>2, "timeInBed"=>0, "createdAt"=>"2015-12-03T15:35:42.903Z", "updatedAt"=>"2015-12-03T15:35:42.903Z", "humanId"=>"3"}
{"id"=>"4", "userId"=>"4", "day"=>"2015-12-02", "startTime"=>"2015-12-02T09:17:33.000Z", "endTime"=>"2015-12-02T15:27:33.000Z", "tzOffset"=>"-08:00", "source"=>"misfit", "mainSleep"=>true, "timeAsleep"=>370, "timeAwake"=>0, "efficiency"=>0, "timeToFallAsleep"=>0, "timeAfterWakeup"=>0, "numberOfWakeups"=>0, "timeInBed"=>0, "createdAt"=>"2015-12-03T04:00:02.050Z", "updatedAt"=>"2015-12-03T04:00:02.050Z", "humanId"=>"4"}
{"id"=>"5", "userId"=>"5", "day"=>"2015-12-01", "startTime"=>"2015-12-01T05:45:36.000Z", "endTime"=>"2015-12-01T13:50:36.000Z", "tzOffset"=>"-08:00", "source"=>"misfit", "mainSleep"=>true, "timeAsleep"=>485, "timeAwake"=>0, "efficiency"=>0, "timeToFallAsleep"=>0, "timeAfterWakeup"=>0, "numberOfWakeups"=>0, "timeInBed"=>0, "createdAt"=>"2015-12-01T17:50:20.168Z", "updatedAt"=>"2015-12-01T17:50:20.168Z", "humanId"=>"5"}
{"id"=>"6", "userId"=>"6", "day"=>"2015-11-30", "startTime"=>"2015-11-30T05:57:38.000Z", "endTime"=>"2015-11-30T14:23:38.000Z", "tzOffset"=>"-08:00", "source"=>"misfit", "mainSleep"=>true, "timeAsleep"=>499, "timeAwake"=>7, "efficiency"=>0, "timeToFallAsleep"=>0, "timeAfterWakeup"=>0, "numberOfWakeups"=>1, "timeInBed"=>0, "createdAt"=>"2015-11-30T16:18:38.276Z", "updatedAt"=>"2015-11-30T16:18:38.276Z", "humanId"=>"6"}
Oscar Mederos

Here is how you can do it:

array.max_by {|x| x['timeAsleep']} # max
array.min_by {|x| x['timeAsleep']} # min
array.map {|x| x['timeAsleep']}.reduce(:+) / array.size.to_f # average

You can read about map and reduce on the documentation

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find the largest value for an array of hashes with common keys?

From Dev

Find the smallest and largest value in an array with JavaScript

From Dev

Find the average. largest, smallest number and mode of a int array

From Dev

Get the largest, average and smallest value

From Dev

Find largest and smallest numbers in an array

From Dev

Average of Smallest and Largest

From Dev

Switching the smallest and largest value in an object array in Java

From Dev

Find largest average set of results in array

From Dev

Find duplicates in array of hashes on specific keys

From Dev

Given an array of integers, find the second largest and second smallest within the array

From Dev

Find second largest value in array

From Java

How do i find the smallest or largest value in a list in Python

From Dev

find the largest and smallest value in a column that corresponds with another column

From Dev

C find the 2 largest numbers and 2 smallest numbers in float array

From Dev

Find largest and smallest number in array using pointers in C

From Dev

Rails Ruby array of hashes merged by key to average value

From Dev

Find value, put it in an array and find the smallest value in the array VBA

From Dev

Find the smallest value in an array excluding 0

From Dev

Find the smallest value in an array excluding 0

From Dev

combine keys in array of hashes

From Dev

How to find array index of largest value?

From Dev

Find out largest string value size for keys in Redis database

From Dev

How do I find the largest value smaller than or equal to X and the smallest value greater than or equal to X?

From Dev

Storing Variable value in an Array to find the Average Speed

From Dev

Partial sorting to find the kth largest/smallest elements

From Dev

Partial sorting to find the kth largest/smallest elements

From Dev

Find the second largest and smallest number in a list

From Dev

Add hashes with duplicate keys to array

From Dev

Find smallest value with tolerance

Related Related

  1. 1

    Find the largest value for an array of hashes with common keys?

  2. 2

    Find the smallest and largest value in an array with JavaScript

  3. 3

    Find the average. largest, smallest number and mode of a int array

  4. 4

    Get the largest, average and smallest value

  5. 5

    Find largest and smallest numbers in an array

  6. 6

    Average of Smallest and Largest

  7. 7

    Switching the smallest and largest value in an object array in Java

  8. 8

    Find largest average set of results in array

  9. 9

    Find duplicates in array of hashes on specific keys

  10. 10

    Given an array of integers, find the second largest and second smallest within the array

  11. 11

    Find second largest value in array

  12. 12

    How do i find the smallest or largest value in a list in Python

  13. 13

    find the largest and smallest value in a column that corresponds with another column

  14. 14

    C find the 2 largest numbers and 2 smallest numbers in float array

  15. 15

    Find largest and smallest number in array using pointers in C

  16. 16

    Rails Ruby array of hashes merged by key to average value

  17. 17

    Find value, put it in an array and find the smallest value in the array VBA

  18. 18

    Find the smallest value in an array excluding 0

  19. 19

    Find the smallest value in an array excluding 0

  20. 20

    combine keys in array of hashes

  21. 21

    How to find array index of largest value?

  22. 22

    Find out largest string value size for keys in Redis database

  23. 23

    How do I find the largest value smaller than or equal to X and the smallest value greater than or equal to X?

  24. 24

    Storing Variable value in an Array to find the Average Speed

  25. 25

    Partial sorting to find the kth largest/smallest elements

  26. 26

    Partial sorting to find the kth largest/smallest elements

  27. 27

    Find the second largest and smallest number in a list

  28. 28

    Add hashes with duplicate keys to array

  29. 29

    Find smallest value with tolerance

HotTag

Archive