简体   繁体   English

如何收集和组合多个数组进行计算?

[英]How do I collect and combine multiple arrays for calculation?

I am collecting the values for a specific column from a named_scope as follows: 我正在从named_scope收集特定列的值,如下所示:

a = survey_job.survey_responses.collect(&:base_pay)

This gives me a numeric array for example (1,2,3,4,5). 这给了我一个数字数组,例如(1,2,3,4,5)。 I can then pass this array into various functions I have created to retrieve the mean, median, standard deviation of the number set. 然后,我可以将此数组传递给我创建的各种函数,以检索数字集的均值,中位数,标准差。 This all works fine however I now need to start combining multiple columns of data to carry out the same types of calculation. 一切正常,但是我现在需要开始组合多列数据以执行相同类型的计算。

I need to collect the details of perhaps three fields as follows: 我需要收集以下三个字段的详细信息:

survey_job.survey_responses.collect(&:base_pay)
survey_job.survey_responses.collect(&:bonus_pay)
survey_job.survey_responses.collect(&:overtime_pay)

This will give me 3 arrays. 这将给我3个数组。 I then need to combine these into a single array by adding each of the matching values together - ie add the first result from each array, the second result from each array and so on so I have an array of the totals. 然后,我需要通过将每个匹配值相加在一起将它们组合成一个数组-即将每个数组的第一个结果相加,每个数组的第二个结果相加,依此类推,这样我就得到了总计的数组。

How do I create a method which will collect all of this data together and how do I call it from the view template? 如何创建将所有这些数据收集在一起的方法,以及如何从视图模板中调用它?

Really appreciate any help on this one... 非常感谢对此的任何帮助...

Thanks 谢谢

Simon 西蒙

s = survey_job.survey_responses
pay = s.collect(&:base_pay).zip(s.collect(&:bonus_pay), s.collect(&:overtime_pay))
pay.map{|i| i.compact.inject(&:+) }

Do that, but with meaningful variable names and I think it will work. 这样做,但是使用有意义的变量名,我认为它将起作用。

Define a normal method in app/helpers/_helper.rb and it will work in the view 在app / helpers / _helper.rb中定义一个普通方法,它将在视图中工作

Edit: now it works if they contain nil or are of different sizes (as long as the longest array is the one on which zip is called. 编辑:现在,如果它们包含nil或具有不同的大小(只要最长的数组是调用zip的数组),它就可以工作。

Here's a method that will combine an arbitrary number of arrays by taking the sum at each index. 这是一种通过取每个索引的和来组合任意数量数组的方法。 It'll allow each array to be of different length, too. 它也将允许每个数组具有不同的长度。

def combine(*arrays)
  # Get the length of the largest array, that'll be the number of iterations needed
  maxlen = arrays.map(&:length).max
  out = []

  maxlen.times do |i|
    # Push the sum of all array elements at a given index to the result array
    out.push( arrays.map{|a| a[i]}.inject(0) { |memo, value| memo += value.to_i } )
  end

  out
end

Then, in the controller, you could do 然后,在控制器中,您可以执行

base_pay = survey_job.survey_responses.collect(&:base_pay)
bonus_pay = survey_job.survey_responses.collect(&:bonus_pay)
overtime_pay = survey_job.survey_responses.collect(&:overtime_pay)

@total_pay = combine(base_pay, bonus_pay, overtime_pay)

And then refer to @total_pay as needed in your view. 然后根据需要参考@total_pay

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM