2016-05-10 14 views
0

Da ein Array von HashesWie Rubin Hash basierend auf dem Wert einiger Tasten

arr = [{'city' => 'Bangalore','device' => 'desktop','users' => '20'}, 
     {'city' => 'Bangalore','device' => 'tablet','users' => '20'}, 
     {'city' => 'Bangalore','device' => 'mobile','users' => '20'}, 
     {'city' => 'Pune','device' => 'desktop','users' => '20'}, 
     {'city' => 'Pune','device' => 'tablet','users' => '20'}, 
     {'city' => 'Pune','device' => 'mobile','users' => '20'}, 
     {'city' => 'Mumbai','device' => 'desktop','users' => '20'}, 
     {'city' => 'Mumbai','device' => 'tablet','users' => '20'}, 
     {'city' => 'Mumbai','device' => 'mobile','users' => '20'}]  

Wie kann ich produziere die folgenden Array von Hashes aggregieren?

[{'city' => 'Bangalore', 'users' => '60'}, 
{'city' => 'Pune', 'users' => '60'}, 
{'city' => 'Mumbai','users' => '60'}] 
+0

ist diese aktive Datensatzobjekt? – uzaif

+0

Haben Sie etwas versucht? –

+0

Es ist keine aktive Aufzeichnung, ich habe jede Schleife verwendet und ein neues Objekt erstellt, aber ich möchte einen besseren Weg kennen – Neevany

Antwort

0

Sie es mögen:

hash = [{'city' => 'Bangalore','device' => 'desktop','users' => '20'}, {'city' => 'Bangalore','device' => 'tablet','users' => '20'}, {'city' => 'Bangalore','device' => 'mobile','users' => '20'}, {'city' => 'Pune','device' => 'desktop','users' => '20'}, 
{'city' => 'Pune','device' => 'tablet','users' => '20'}, {'city' => 'Pune','device' => 'mobile','users' => '20'}, {'city' => 'Mumbai','device' => 'desktop','users' => '20'}, 
{'city' => 'Pune','device' => 'tablet','users' => '20'}, {'city' => 'Mumbai','device' => 'mobile','users' => '20'}] 

new_hash = hash.map{|obj| {:city => obj[:city], :users => obj[:users]}} 
+1

"Hash" ist vielleicht nicht der beste Name für ein Array. –

0

Try this

def setter hash, hash_array_new 
    new_hash = {} 
    new_hash["users"] = hash["users"].to_i 
    new_hash["city"] = hash["city"] 
    hash_array_new << new_hash 
end 

hash_array_new = [] 
hash_array.each do |hash| 
    count = 0 
    hash_array_new.each do |new_hash| 
    if hash["city"] == new_hash["city"] 
     new_hash["users"] += hash["users"].to_i 
     count = count+1 
    end 
    end 
    if count == 0 
    setter hash, hash_array_new 
    end 
    if hash_array_new.blank? 
    setter hash, hash_array_new 
    end 
end 


puts hash_array_new //display resultant array 
0
arr.each_with_object(Hash.new(0)) { |g,h| h[g["city"]] += g["users"].to_i }. 
    map { |city,users| { 'city' => city, 'users' => users.to_s } } 
    #=> [{"city"=>"Bangalore", "users"=>"60"}, 
    # {"city"=>"Pune", "users"=>"60"}, 
    # {"city"=>"Mumbai", "users"=>"60"}] 

Hash.new(0) manchmal als das Zählen Hash bezeichnet wird. Null ist der Standardwert. (Siehe Hash::new.) Das bedeutet, dass für

h[g["city"]] += g["users"].to_i 

die

h[g["city"]] = h[g["city"]] + g["users"].to_i 

h[g["city"]] auf der rechten Seite zu

erweitert mit dem Standardwert von Null ersetzt wird, wenn h keinen Schlüssel g["city"] hat.

Beachten Sie, dass

arr.each_with_object(Hash.new(0)) { |g,h| h[g["city"]] += g["users"].to_i } 
    #=> {"Bangalore"=>60, "Pune"=>60, "Mumbai"=>60} 
Verwandte Themen