ruby on rails - Change the structure of hash -


i have hash below:

{ '3.2':{  'abc-4536':{    "a" : "sfsdfs",    "b" : "qweqda",    "pa": "printer"     },   'abc-2345':{     "a": "sdfswer",     "b": "werwewe",     "pa": "newspaper"     },   'abc-4536':{     "a" : "sfsdfs",     "b" : "qweqda",     "pa": "printer"     },    ...   } } 

so, have arrange this:

{ '3.2':{   "printer":{     'count': 2    },   "newspaper":{     'count': 3    },   } } 

i have count , group "pa" key inside '3.2'. ideas ?

assuming have array of specified objects:

array.map{ |x| { x.first[0] => x.first[1].map { |k,v| [v['pa'], "calculated value"] }.to_h } } 

or if have 1 hash:

{ hash.first[0] => hash.first[1].map { |k,v| [v['pa'], "calculated value"] }.to_h }  

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -