arr = [1,2,1,3,5,2,4]
Run Code Online (Sandbox Code Playgroud)
如何通过排序按组值计算数组?我需要以下输出:
x[1] = 2
x[2] = 2
x[3] = 1
x[4] = 1
x[5] = 1
Run Code Online (Sandbox Code Playgroud) 我有一系列哈希
[ {:name => "bob", :type => "some", :product => "apples"},
{:name => "ted", :type => "other", :product => "apples"},....
{:name => "Will", :type => "none", :product => "oranges"} ]
Run Code Online (Sandbox Code Playgroud)
并且想知道是否有一种简单的方法来计算产品的数量并存储计数以及数组或散列中的值.
我希望结果如下:
@products = [{"apples" => 2, "oranges => 1", ...}]
Run Code Online (Sandbox Code Playgroud)