DollarChills
DollarChills

Reputation: 1086

Rails array exclude

I have an array that feature date + time and a temperature.

I am looking to count the number of temperature below 25 degrees that fall between a specific time frame (16:00:00 - 20:00:00), but unsure on how to best tackle this scenario.

Data from json file

{"status": "ok", "data": [{"2014-06-16 16:00:00": 24.2},{"2014-06-17 12:00:00": 30.2},{"2014-06-18 17:00:00": 42.9}]} etc

Controller

@data = JSON.parse(open(@temperature.url).read)

dates = []
temps = []

@data['data'].each do |data|
 dates << data.keys
 temps << data.values
end

@nights25 = dates.flatten.count {|i| ["16:", "17:", "18:", "19:", "20:"].include?(i) if i.temps > 25 end } 

Upvotes: 1

Views: 255

Answers (1)

railscard
railscard

Reputation: 1848

Maybe something like this:

temp = @data['data'].reduce({}, :merge)    
temp.count { |time, deg| (16..20).include?(Time.parse(time).hour) && deg < 25 }

Upvotes: 2

Related Questions