hi,

please consider the below array

[{“a”=>“b”,“c”=>“d”},{“a”=>“b”,“c”=>“12”},{“a”=>“k”,“c”=>“as”},{“a”=>“k”,“c”=>“like”}]

Now I need to split this array as wherever the value of key “a” is

equal I need to keep that in separate array, that means the array has

to appear like below

[{“a”=>“b”,“c”=>“d”},{“a”=>“b”,“c”=>“12”}] value “b” is common

[{“a”=>“k”,“c”=>“as”},{“a”=>“k”,“c”=>“like”}] value k is common

I know this can be achieved through by looping through each element and

writing some condition, but I would like to know, Is there any built in

function to do this job, Can you please give me?

Raja gopalan wrote in post #1152426:

I know this can be achieved through by looping through each element and

writing some condition, but I would like to know, Is there any built in

function to do this job, Can you please give me?

a=[{“a”=>“b”,“c”=>“d”},{“a”=>“b”,“c”=>“12”},{“a”=>“k”,“c”=>“as”},{“a”=>“k”,“c”=>“like”}]

a.group_by { |x| x[“a”]}

=> {“b”=>[{“a”=>“b”, “c”=>“d”}, {“a”=>“b”, “c”=>“12”}], “k”=>[{“a”=>“k”,

“c”=>“as”}, {“a”=>“k”, “c”=>“like”}]}

a.group_by { |x| x[“a”]}.values

=> [[{“a”=>“b”, “c”=>“d”}, {“a”=>“b”, “c”=>“12”}], [{“a”=>“k”,

“c”=>“as”}, {“a”=>“k”, “c”=>“like”}]]

so, no loop and no condition…

Hi,

thank you, I found another function ‘select’.