查找indentical哈希数组中特定条目的最大整数值

时间:2023-02-11 04:45:53

Let's say I have a data structure such as this:

假设我有一个这样的数据结构:

[[{:value=>"First Item", :value_length=>10, :index=>0},
  {:value=>"1111", :value_length=>4, :array_index=>1}],
 [{:value=>"Second Item", :value_length=>11, :index=>0},
  {:value=>"2222", :value_length=>4, :array_index=>1}],
 [{:value=>"Third Item", :value_length=>10, :index=>0},
  {:value=>"3333", :value_length=>4, :index=>1}],
 [{:value=>"Fourth Item", :value_length=>11, :index=>0},
  {:value=>"4444", :value_length=>4, :index=>1}]]

(console output)

(控制台输出)

How could I get the largest :value_length in the array?

我怎么能得到数组中最大的:value_length?

2 个解决方案

#1


2  

Do this:

做这个:

your_array.flatten.map{|h| h[:value_length]}.max
# => 11

#2


1  

I was sad because @sawa was faster. But I will still answer in case you want to get the full hash.

我很伤心,因为@sawa更快。但是我仍然会回答你想获得完整哈希的情况。

your_array.flatten.max {|x, y| x[:value_length] <=> y[:value_length] }
#=> {:value=>"Second Item", :value_length=>11, :index=>0}

#1


2  

Do this:

做这个:

your_array.flatten.map{|h| h[:value_length]}.max
# => 11

#2


1  

I was sad because @sawa was faster. But I will still answer in case you want to get the full hash.

我很伤心,因为@sawa更快。但是我仍然会回答你想获得完整哈希的情况。

your_array.flatten.max {|x, y| x[:value_length] <=> y[:value_length] }
#=> {:value=>"Second Item", :value_length=>11, :index=>0}