and i have
"city_id":[
"1",131,
"2",96,
"3",11,
"4",10,
"7",8,
"6",6,
"15",5,
"10",4,
"34",4,
"36",3,
"59",3,
"71",1]},
In quotes is city_id and on the right is the job count of that city
I want to process them into a hash like this
{:region_1=>
{["city_name", "slug"]=>jobs count,
["city_name", "slug"]=>jobs count,
["city_name", "slug"]=>jobs count},
:region_2=>
{["city_name", "slug"]=>jobs count,
["city_name", "slug"]=>jobs count,
["city_name", "slug"]=>jobs count},
...
}
This isn't the cleanest way to do, but it could work:
final_hash = {}
Hash[*city_id.flatten(1)].each do |city_id, job_count|
city = City.find(city_id)
region = city.region
slug = city.slug
city_name = city.name
final_hash[region] = Hash.new unless final_hash.key?(region)
final_hash[region][[city_name, slug]] = job_count
end
This makes your strange arraylist to a hash:
Hash[*city_id.flatten(1)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.