How to replace a hash key with another key

后端 未结 11 1493
青春惊慌失措
青春惊慌失措 2020-12-04 07:36

I have a condition where, I get a hash

  hash = {\"_id\"=>\"4de7140772f8be03da000018\", .....}

and I want this hash as

         


        
11条回答
  •  感情败类
    2020-12-04 08:10

    you can do

    hash.inject({}){|option, (k,v) | option["id"] = v if k == "_id"; option}
    

    This should work for your case!

提交回复
热议问题