One minute
Convert Array of Hashes to Hash π€ΈπΌββοΈ
In your Ruby on Rails project, you can override array.rb
file and add few good methods there π
i.e. - convert array of hashes to hash.
Path - lib/array.rb
β οΈ Don't forget to require it in application.rb
Code snippet π
def convert_to_hash
Hash[self.map(&:values).map(&:flatten)] rescue {}
end
β― array = [{ key: 1, value: 'one' }, { key: 2, value: 'two' }]
β― array.convert_to_hash
β― { 1 => "one", 2 => "two" }
Read other posts
comments powered by Disqus