1

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 } 

1 Answer 1

2

Maybe something like this:

temp = @data['data'].reduce({}, :merge)    
temp.count { |time, deg| (16..20).include?(Time.parse(time).hour) && deg < 25 }
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I completely missed the time part!
Seems to have worked. Thanks @railscard. I'll have to do some testing as the data is huge. I have never seen the reduce option before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.