mardi 31 mars 2015

How to process array of hashes in a better way

Below is the array of hashes from the PGResult set.



result = [
{"type" => 1 , "original" => "true", "count" => "10"},
{"type" => 1 , "original" => "false", "count" => "20"},
{"type" => 2 , "original" => "false", "count" => "30"},
{"type" => 2 , "original" => "true", "count" => "40"},
{"type" => 3 , "original" => "true", "count" => "50"},
{"type" => 3 , "original" => "false", "count" => "60"}
]


I want to process the above array of hashes to the below format. total_count = count of original("true") - count of original("false") for each type!



result = [
{
"type" => 1, "total_count" => "10",
"type" => 2, "total_count" => "10",
"type" => 3, "total_count" => "-10",
}

]


any hint to process the above array ?


Aucun commentaire:

Enregistrer un commentaire