Rails merging two arrays of hashes?
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I'm having a hard time grasping the logic I'd need to merge two arrays of hashes, It seems I've asked t his question a while back in sort of a different way, I've also tried a few other things like the answers offered here: merging arrays of hashes , it looks like you first group by timestamp and then merge the values: (a1+a2).group_by{|h| h[:timestamp]}.map{|k,v| v.reduce(:merge)}
|
How to merge arrays of hashes based on keys of hashes in Rails
Date : March 29 2020, 07:55 AM
I wish this help you I want to merge these two arrays based on uniqueness: (first_array + second_array).uniq{|hash| hash.keys.first}
|
Improvements in code having iteration of nested hashes and arrays of input
Tag : ruby , By : lonehunter01
Date : March 29 2020, 07:55 AM
I hope this helps you . I think a big problem comes from the data you're passing in. Take for example, a working solution for your final issue. To get the data for a single season, you can use: def average_age_for(data, season)
contestants = data[season]
contestants.sum { |contestant| contestant[:age].to_f } / contestants.count
end
average_age_for(thebatchelor, :"season 30")
#=> 26.5
thebachelor = {
"season 30" => [
{
"name" => "Beth Smalls",
"age" => "26",
"hometown" => "Great Falls, Virginia",
"occupation" => "Nanny/Freelance Journalist",
"status" => "Winner"
},
{
"name" => "Becca Tilley",
"age" => "27",
"hometown" => "Shreveport, Louisiana",
"occupation" => "Chiropractic Assistant",
"status" => "Eliminated Week 8"
}
],
"season 29" => [
{
"name" => "Ashley Yeats",
"age" => "24",
"hometown" => "Denver, Colorado",
"occupation" => "Dental Assitant",
"status" => "Winner"
},
{
"name" => "Sam Grover",
"age" => "29",
"hometown" => "New York, New York",
"occupation" => "Entertainer",
"status" => "Eliminated Week 6"
}
]
}
def average_age_for(data, season)
contestants = data[season]
# vvvvvvv
contestants.sum { |contestant| contestant["age"].to_f } / contestants.count
# ^^^^^^^
end
def get_first_name_of_season_winner(data, season)
data[season].detect { |contestant| contestant["status"] == "Winner" }["name"].split.first
end
get_first_name_of_season_winner(thebachelor, "season 29")
#=> "Ashley"
def get_contestant_name(data, occupation)
data.values.flatten.detect { |contestant| contestant["occupation"] == occupation }
end
get_contestant_name(thebachelor, "Chiropractic Assistant")
#=> {"name"=>"Becca Tilley", "age"=>"27", "hometown"=>"Shreveport, Louisiana", "occupation"=>"Chiropractic Assistant", "status"=>"Eliminated Week 8"}
def count_contestant_by_hometown(data, town)
data.values.flatten.select { |contestant| contestant["hometown"] == town }.count
end
count_contestant_by_hometown(thebachelor, "New York, New York")
#=> 1
def get_occupation(data, hometown)
data.values.flatten.detect { |contestant| contestant["hometown"] == hometown }["occupation"]
end
get_occupation(thebachelor, "New York, New York")
#=> "Entertainer"
|
How to access hashes whose keys are arrays in Rails?
Date : March 29 2020, 07:55 AM
it should still fix some issue I am trying to access data that has a structure like this (the status of each user on specific dates). As you can see, the hash keys are all arrays. This data has been retrieved from the DB using group_by. , This should work: new_data = {}
data.each do |k, v|
new_data[k.first] ||= []
new_data[k.first] << { k.last => v}
end
|
How does Rails group arrays of hashes during form submission?
Date : March 29 2020, 07:55 AM
|