Consolidating Multidimensional Array in Ruby -


i have array of "votes" set [id, rating] inside array

[["1250", "5"],  ["1250", "5"],  ["1250", "5"],  ["1250", "5"],  ["1250", "4"],  ["1250", "5"],  ["1250", "5"],  ["1252", "2"],  ["1252", "5"],  ["1252", "4"],  ["1252", "3"],  ["1252", "5"],  ["1252", "4"],  ["1252", "4"],  ["1254", "5"],  ["1254", "4"],  ["1254", "4"],  ["1257", "5"],  ["1257", "5"],  ["1257", "4"],  ["1257", "5"],  ...] 

there multiples of x want merge , keep y's pertaining x accessible. have average votes (y) particular id (x) , unsure how that. in addition these votes (y) have "weighted" different amounts later, think keeping access them helpful down road.

frankly don't know called don't know :/ tried merging, pushing y's onto array[x], , complicated 'for unique x |y|'. stumped on how handle problem.

end goal this:

[["1250", ["5", "5", "5", "4", "5", "5"]],  ["1252", ["2", "5", "4", "3", "5", "4", "4"]],  ["1254", ["5", "4", "4"]],  ["1257", ["5", "5", "4", "5"]], ...] 

[["1250", "5"],  ["1250", "5"],  ["1250", "5"],  ["1250", "5"],  ["1250", "4"],  ["1250", "5"],  ["1250", "5"],  ["1252", "2"],  ["1252", "5"],  ["1252", "4"],  ["1252", "3"],  ["1252", "5"],  ["1252", "4"],  ["1252", "4"],  ["1254", "5"],  ["1254", "4"],  ["1254", "4"],  ["1257", "5"],  ["1257", "5"],  ["1257", "4"],  ["1257", "5"]]  hsh = hash.new{|h,k| h[k] = []}  # hsh stores key empty array if not "know" key votes.each_with_object(hsh){|(id, vote), h| h[id] << vote} # add vote array when hsh "knows" key.  p hsh # =>{"1250"=>["5", "5", "5", "5", "4", "5", "5"], "1252"=>["2", "5", "4", "3", "5", "4", "4"]...} 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -