如何在哈希中给定多个其他值,在哈希数组中查找并返回哈希值

时间:2022-12-07 13:35:23

I have this array of hashes:

我有这个哈希数组:

results = [
   {"day"=>"2012-08-15", "name"=>"John", "calls"=>"5"},
   {"day"=>"2012-08-15", "name"=>"Bill", "calls"=>"8"},
   {"day"=>"2012-08-16", "name"=>"Bill", "calls"=>"11"},
]

How can I search the results to find how many calls Bill made on the 15th?

如何搜索结果以查找Bill在15日拨打了多少电话?

After reading the answers to "Ruby easy search for key-value pair in an array of hashes", I think it might involve expanding upon the following find statement:

在阅读了“Ruby在一系列哈希中轻松搜索键值对”的答案之后,我认为它可能涉及扩展以下查找语句:

results.find { |h| h['day'] == '2012-08-15' }['calls']

5 个解决方案

#1


15  

You're on the right track!

你走在正确的轨道上!

results.find {|i| i["day"] == "2012-08-15" and i["name"] == "Bill"}["calls"]
# => "8"

#2


1  

results.select { |h| h['day'] == '2012-08-15' && h['name'] == 'Bill' }
  .reduce(0) { |res,h| res += h['calls'].to_i } #=> 8

#3


0  

A Really clumsy implementation ;)

一个非常笨拙的实现;)

def get_calls(hash,name,date) 
 hash.map{|result| result['calls'].to_i if result['day'] == date && result["name"] == name}.compact.reduce(:+)
end

date = "2012-08-15"
name = "Bill"

puts get_calls(results, name, date)
=> 8 

#4


0  

Or another possible way, but a little worse, using inject:

或者另一种可能的方式,但更糟糕的是,使用注入:

results.inject(0) { |number_of_calls, arr_element| arr_element['day'] == '2012-08-15' ? number_of_calls += 1 : number_of_calls += 0  }

Note that you have to set number_of_calls in each iteration, otherwise it will not work, for example this does NOT work:

请注意,您必须在每次迭代中设置number_of_calls,否则它将无法工作,例如,这不起作用:

p results.inject(0) { |number_of_calls, arr_element| number_of_calls += 1 if arr_element['day'] == '2012-08-15'}

#5


0  

Actually, "reduce" or "inject" is specifically for this exact operation (To reduce the contents of an enumerable down into a single value:

实际上,“reduce”或“inject”专门用于这个精确的操作(要将可枚举的内容减少为单个值:

results.reduce(0) do |count, value|
  count + ( value["name"]=="Bill" && value["day"] == "2012-08-15" ? value["calls"].to_i : 0)
end

Nice writeup here: "Understanding map and reduce"

好的写在这里:“了解地图并减少”

#1


15  

You're on the right track!

你走在正确的轨道上!

results.find {|i| i["day"] == "2012-08-15" and i["name"] == "Bill"}["calls"]
# => "8"

#2


1  

results.select { |h| h['day'] == '2012-08-15' && h['name'] == 'Bill' }
  .reduce(0) { |res,h| res += h['calls'].to_i } #=> 8

#3


0  

A Really clumsy implementation ;)

一个非常笨拙的实现;)

def get_calls(hash,name,date) 
 hash.map{|result| result['calls'].to_i if result['day'] == date && result["name"] == name}.compact.reduce(:+)
end

date = "2012-08-15"
name = "Bill"

puts get_calls(results, name, date)
=> 8 

#4


0  

Or another possible way, but a little worse, using inject:

或者另一种可能的方式,但更糟糕的是,使用注入:

results.inject(0) { |number_of_calls, arr_element| arr_element['day'] == '2012-08-15' ? number_of_calls += 1 : number_of_calls += 0  }

Note that you have to set number_of_calls in each iteration, otherwise it will not work, for example this does NOT work:

请注意,您必须在每次迭代中设置number_of_calls,否则它将无法工作,例如,这不起作用:

p results.inject(0) { |number_of_calls, arr_element| number_of_calls += 1 if arr_element['day'] == '2012-08-15'}

#5


0  

Actually, "reduce" or "inject" is specifically for this exact operation (To reduce the contents of an enumerable down into a single value:

实际上,“reduce”或“inject”专门用于这个精确的操作(要将可枚举的内容减少为单个值:

results.reduce(0) do |count, value|
  count + ( value["name"]=="Bill" && value["day"] == "2012-08-15" ? value["calls"].to_i : 0)
end

Nice writeup here: "Understanding map and reduce"

好的写在这里:“了解地图并减少”