Adding new keys to a Array Of Hashes
Solution 1:
I would probably:
- Use
map
to iterate over the elements inlist
and build a new array at the same time - Since each element is a hash with one item, destruct that into a key and value
- Build the new hash in the correct format
new_list = list.map do |hash|
# e.g. key = "Mary", value = "Die Hard"
key, value = hash.first
{name: key, film: value}
end
This assumes that each hash in the list will have only one item (as you've shown in your example in the question) - you may want to validate this elsewhere, since this solution would ignore any other items in the hash past the first.