简体   繁体   English

在Ruby on Rails中将3个数组合并到Hash中

[英]Combine 3 arrays into Hash in Ruby on Rails

I have 3 arrays: min , max and regions . 我有3个数组: minmaxregions I want to create a hash for each region with corresponding min and max value. 我想为每个region创建具有对应的minmax的哈希。 Something like this: 像这样:

regions=["Region 1","Region 2",....]
min=["100","200",...]
max=["500","300",...]

#=> {"Region1"=>["100", "500"], "Region 2"=>["200", "300"], ...}

Here is my code: 这是我的代码:

@min = params[:min]
@max = params[:max]
@regions = params[:regions]

I have tried this so far, but didn't work: 到目前为止,我已经尝试过了,但是没有用:

@range_map = Hash[@regions.map{|r| [r, [@min.each.to_i,@max.each.to_i]]}]

All I want is a hash from 3 array and min and max to be converted to integer . 我想要的只是一个3数组的哈希,将min和max转换为integer

You can try something like this, using zip and transpose : 您可以尝试使用ziptranspose这样的事情:

range_map = regions.zip([min.map(&:to_i), max.map(&:to_i)].transpose).to_h

#=> {"Region1"=>[100, 500], "Region2"=>[200, 300]}

Demonstration 示范

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

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