Posts

Showing posts from February 21, 2019

Group array of hashes on key and append nested array on another hash key

Image
-2 I need to reduce an array of hashes into a single hash if that hash key matches. my_array is an array of hashes, each of which has two keys: one an active record object, the other an array of different active record objects, similar to: my_array = [ { first_hash_key: {id: 1, title: "my title"}, second_hash_key: ["stuff", "more stuff", "more stuff"] }, { first_hash_key: {id: 1, title: "my title"}, second_hash_key: ["stuff 3", "uniq stuff 2"] }, { first_hash_key: {id: 2, title: "my other title"}, second_hash_key: ["interesting stuff", "uniq stuff"] } ] I want to combine hashes by first_hash_key[:id] , add each item in hash_key_two to an array, not overwrite t