Created
May 15, 2014 16:42
-
-
Save michaeldelorenzo/7768c7c67b88229ec16c to your computer and use it in GitHub Desktop.
Recursively removes key-value pairs from a Hash using lambda.
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
remove_id_attr = lambda do |obj| | |
if obj.is_a?(Hash) | |
obj.keys.each do |_k| | |
if _k.to_s.eql?('_id') | |
obj.delete(_k) | |
elsif obj[_k].is_a?(Hash) | |
remove_id_attr.call(obj[_k]) | |
elsif obj[_k].is_a?(Array) | |
obj[_k].each{|o| remove_id_attr.call(o)} | |
end | |
end | |
end | |
end | |
# remove _id attributes on sub-documents | |
remove_id_attr.call(demo_profile_doc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment