-
-
Save xcobar/22164664b1255aed43e134758449577d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array = [ | |
{ | |
cars: 4, | |
houses: 5, | |
job: {}, | |
fat: {}, | |
}, | |
{ | |
cars: 4, | |
houses: 5, | |
job: {}, | |
fat: {}, | |
}, | |
{ | |
cars: 4, | |
houses: 5, | |
job: {}, | |
fat: {}, | |
}, | |
] | |
#array = array.reduce(Hash.new(0)) { |acc, h| acc.merge(h) { |_key, left, right| left + right } } | |
first, *rest = array | |
first.merge(*rest) { |key, left, right| left + right unless %i[job fat].include?(key) }.compact | |
p array | |
#=> [{:cars=>4, :houses=>5, :job=>{}, :fat=>{}}, {:cars=>4, :houses=>5, :job=>{}, :fat=>{}}, {:cars=>4, :houses=>5, :job=>{}, :fat=>{}}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment