组值数组 - group value in array

- 此内容更新于:2016-02-02
主题:

我希望——:=时间<5=时间>5

原文:
data = [{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>1},
{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>2},
{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>3},
{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>4},
{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>5},
{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>6},
{"discount_code"=>"4567", "affiliate_name"=>"amit", "time"=>4},
{"discount_code"=>"4567", "affiliate_name"=>"amit", "time"=>6},
{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>7}]

I want-

[{"discount_code"=>"LAKR", "total_count"=>7, "up"=>4, "down"=>3, "affiliate_name"=>"Jasbir Singh"},
{"discount_code"=>"4567", "total_count"=>2,"up"=>1, "down"=>1, "affiliate_name"=>"amit"}]

where : up = time < 5 down = time > 5

网友:到目前为止你试过基于回答你之前的问题吗?同时,你需要格式化你的代码片段——当编辑你的问题,有一个工具栏,使用的选项。

(原文:What have you tried so far based on answer to your previous question? Also, you need to format your code snippet - when editing your question, there is a tool bar and use the options from it.)

网友:你能正确格式代码吗?我也确信我最近见过这个问题或类似的贴…

(原文:Can you format your code correctly? Also I'm sure I've seen this question or similar posted recently...)

网友:请阅读如何问一个问题以避免将来选票。

(原文:Please read How to ask a question to avoid down votes in future.)

解决方案:

原文:
data = [{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>1},
{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>2},
{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>3},
{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>4},
{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>5},
{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>6},
{"discount_code"=>"4567", "affiliate_name"=>"amit", "time"=>4},
{"discount_code"=>"4567", "affiliate_name"=>"amit", "time"=>6},
{"discount_code"=>"LAKR", "affiliate_name"=>"Jasbir Singh", "time"=>7}]

#initailizing final array
final_array = Array.new
#grouping the array with discount_code value => returns hash with each discount_code as key and array as a value
grouped_hash = data.group_by { |array| array["discount_code"] }

grouped_hash.each { |k,v|
#initializing time array for each discount code
time_array = Array.new
#initializing hash for the final array
hash1 = Hash.new
        v.each {|inner_array|
                inner_array.each { |k,v|
                        if k != "time" then
                        hash1[k] = v unless hash1.has_key?(k)
                        else time_array.push(v) if k == "time"
                        end
                }
                }
hash1["total_count"] = time_array.length
hash1["up"] = time_array.select { |x| x.to_i<5 }.count
hash1["down"] = time_array.select { |x| x.to_i>=5 }.count
final_array.push(hash1)
 }
puts final_array
网友:非常感谢你…:)

(原文:Thank you so much... :))