@OrdeO
just......newbie

Как получить наибольший элемент в массиве хешей?

Есть массив:

[
  {"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}
]

Как получить название расы(description) с наибольшим значением count, при этом сравнивать нужно только дескрипшны: white, black, asian, hispanic, а остальные игнорировать?
  • Вопрос задан
  • 71 просмотр
Решения вопроса 1
0xD34F
@0xD34F
search = [ 'White', 'Black', 'Asian', 'Hispanic' ]

max = arr
  .select{|n| search.include?(n['description'])}
  .max_by{|n| n['count']}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы