O
O
Ord EO2020-04-23 20:15:35
ruby
Ord EO, 2020-04-23 20:15:35

How to get the largest element in an array of hashes?

There is an array:

[
  {"description"=>"White", "count"=>8573, "tested"=>38415, "deaths"=>621},
  {"description"=>"Black", "count"=>8504, "tested"=>21670, "deaths"=>596},
  {"description"=>"Left Blank", "count"=>9050, "tested"=>82777, "deaths"=>80,},
  {"description"=>"Other", "count"=>1447, "tested"=>7814, "deaths"=>21, "color"=>"rgb(148,103,189)"},
  {"description"=>"Asian", "count"=>1244, "tested"=>3487, "deaths"=>62},
  {"description"=>"Hispanic", "count"=>6195, "tested"=>9958, "deaths"=>18},
  {"description"=>"NH/PI*", "count"=>45, "tested"=>84, "deaths"=>3},
  {"description"=>"AI/AN**", "count"=>50, "tested"=>141, "deaths"=>1}
]

How to get the name of the race (description) with the highest count value, while comparing only descriptions: white, black, asian, hispanic, and ignore the rest?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-04-23
@OrdeO

search = [ 'White', 'Black', 'Asian', 'Hispanic' ]

max = arr
  .select{|n| search.include?(n['description'])}
  .max_by{|n| n['count']}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question