简体   繁体   English

如何将两个不同散列数组的值一起添加?

[英]How do I add values from two different arrays of hashes together?

I have two arrays of hashes. 我有两个哈希数组。 The keys for the hashes are different: 哈希的键是不同的:

player_scores1 = [{:first_name=>"Bruce", :score => 43, :time => 50},
                  {:first_name=>"Clark", :score => 45, :minutes => 20}]

player_scores2 = [{:last_name=>"Wayne", :points => 13, :time => 40},
                  {:last_name=>"Kent", :points => 3, :minutes => 20}]

I'd like to create a new array of hashes which adds up :score and :points together and assign it to a key called :score. 我想创建一个新的哈希数组,它们加起来:得分和:一起指向并将其分配给一个名为:score的键。 I'd also like to combine the :first_name and :last_name and assign it to a key called :full_name. 我还想组合:first_name和:last_name并将其分配给名为:full_name的键。 I want to discard any other keys. 我想丢弃任何其他钥匙。

This would result in this array: 这将导致这个数组:

all_players = [{:full_name => "Bruce Wayne", :score => 56}, 
               {:full_name => "Clark Kent", :score => 48}]

Is there an elegant way to do this? 有一种优雅的方式来做到这一点?

Something like this: 像这样的东西:

player_scores1.zip(player_scores2).map { |a,b|
    {
        :full_name => a[:first_name]+' '+b[:last_name],
        :score => a[:score]+b[:points]
    }
}

The code you're looking for is: 您正在寻找的代码是:

final = []
player_scores1.each_index do |index|
  entry_1 = player_scores1.values(index)
  entry_2 = player_scores2.values(index)[:first_name]
  score = entry_1[:score] + entry_2[:points]
  final << {:full_name => "#{entry_1[:first_name]} #{entry_2[:last_name]}", :score => score }
end

Any suggestions on tightening this up would be much appreciated! 任何关于收紧这一点的建议都将非常感谢!

This works. 这有效。 I don't if that's elegant enough though. 如果那个优雅的话,我不会。

player_scores1 = [{:first_name=>"Bruce", :score => 43, :time => 50},
                  {:first_name=>"Clark", :score => 45, :minutes => 20}]

player_scores2 = [{:last_name=>"Wayne", :points => 13, :time => 40},
                  {:last_name=>"Kent", :points => 3, :minutes => 20}]

p (0...[player_scores1.length, player_scores2.length].min).map {|i| {
    :full_name => player_scores1[i][:first_name] + " " + player_scores2[i][:last_name], 
    :score => player_scores1[i][:score] + player_scores2[i][:points]
}}

This example on Codepad . Codepad上的这个例子。

This uses zip with a block to loop over the hashes, joining the names and summarizing: 这使用带有块的zip来循环哈希,连接名称并总结:

all_players = []
player_scores1.zip(player_scores2) { |a, b| 
  all_players << { 
    :full_name => a[:first_name] + ' ' + b[:last_name],
    :score     => a[:score] + b[:points]
  } 
}
all_players # => [{:full_name=>"Bruce Wayne", :score=>56}, {:full_name=>"Clark Kent", :score=>48}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从具有相同键的嵌套散列和数组中分组和添加值? - How do I group and add values from nested hashes and arrays with same key? 如果我有一组具有相同键和值的散列集合,我如何将另一个值相加? - If I have a collection of hashes with the same key and values, how do I add another value together? 如何将两个散列合并到数组的散列中? - How do I merge two hashes into a hash of arrays? 如何在Ruby Hash中将两个值加在一起? - How do I add the two values together in this Ruby Hash? Ruby:我有两个哈希数组,如何计算两个哈希中具有相同ID的元素? - Ruby: I have two arrays of hashes, how do I count elements with the same id in both hashes? 如何比较两个填充有哈希值的数组,并返回第二个不存在的哈希值? - How do I compare two arrays filled with hashes and return the hashes that are not present in the second? 如何在rails上的ruby中添加和合并来自不同哈希数组的值 - How to add and merge values from different array of hashes in ruby on rails 如何将两个数组加在一起并收集其中一个或两个都可以为null的id - How do I add two arrays together and collect ids where one or both can be null 如何有条件地合并哈希并添加值? - How do I conditionally merge hashes and add values? 如何从哈希提取值到单独的数组? - How to extract values from hashes into separate arrays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM