Hash Array Iteration
Here’s a tricky one:
hash_array = [{"pears" => 1}, {"pears" => 4}, {"apples" => 4}, {"oranges" => 7}]
}]
If you want to consolidate the hashes with the same key while adding their values, use inject and merge:
hash_array.inject do |fruit, amount|
fruit.merge(amount) { |_x, amount_1, amount_2| amount_1 + amount_2 }
end
#=> prints {"pears"=>5, "apples"=>4, "oranges"=>7}
Careful though, the result is now a hash and no longer an array.