Ruby / Rails Groups only Adjacent Array Elements -


i have sorted array full of objects i'd group attribute, if they're adjacent in array.

a sorted start attribute.

a = [{name: "joe", start: "9am", end: "10am"}, {name: "joe", start: "10am", end: "11am"}, {name: "harry", start: "11am", end: "12pm"}, {name: "harry", start: "12pm", end: "1pm"}, {name: "harry", start: "1pm", end: "2pm"}, {name: "joe", start: "2pm", end: "3pm"}, {name: "joe", start: "3pm", end: "4pm"}]

i group adjacent objects name attribute results looks like:

a = [[{name: "joe", start: "9am", end: "10am"},{name: "joe", start: "10am", end: "11am"}],     [{name: "harry", start: "11am", end: "12pm"},{name: "harry", start: "12pm", end: "1pm"},{name: "harry", start: "1pm", end: "2pm"}],     [{name: "joe", start: "2pm", end: "3pm"},{name: "joe", start: "3pm", end: "4pm"}]] 

this should do:

a.chunk {|hash| hash[:name]}.map(&:last) 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -