ruby - How to count values in a array of hashes -
I have an array of hash
[{: name = & gt; "Bob" ,: Type = & gt; "Something" ,: Product = & gt; "Apple"}, {: name = & gt; "TED" ,: Type = & gt; "Other" ,: Product = & gt; "Apple"}, .... {: name = & gt; "Will" ,: Type = & gt; "None" ,: product = & gt; "Oranges"}] And wondering if there is an easy way to count the number of the product and count the value as well as calculate the value in an array or hush.
The result should be something like this:
@products = [{"apples" => 2, "orange = & gt; 1", ...}]
Can do as
array = [[name :: name = & gt; "Bob" ,: Type = & gt; "Something" ,: Product = & gt; "Apple"}, {: name = & gt; "TED" ,: Type = & gt; "Other" ,: Product = & gt; "Apple"}, {: name = & gt; "Will" ,: Type = & gt; "None" ,: product = & gt; "Oranges"}] array.each_with_object (hash new (0)) {| H1, h2 H2 [H1 [: Products]] + = 1} # = & gt; {"Apple" = & gt; 2, "oranges" = & gt; 1}
Comments
Post a Comment