我试图将二维数组转换为哈希数组。 这是我的阵列:
[[8765, 105191, 2.0, 1582.1], [4321, 62870, 2.0, 603.24], [1234, 62870, 2.0, 500.24]]
我试图将其转换为哈希值数组。像这样:
[{"sales_user_id"=>"8765", "user_id"=>"105191", "month"=>"2", "sum"=>"1582.1"},
{"sales_user_id"=>"4321", "user_id"=>"62870", "month"=>"2", "sum"=>"603.24"},
{"sales_user_id"=>"1234", "user_id"=>"62870", "month"=>"2", "sum"=>"500.24"}]
我分解每个数组并为数组中的值分配一个键。我对红宝石还有点新意,我不知道从哪里开始。
答案 0 :(得分:3)
values = [ [ 8765, 105191, 2.0, 1582.1 ],
[ 4321, 62870, 2.0, 603.24 ],
[ 1234, 62870, 2.0, 500.24 ] ]
keys = %w[sales_user_id user_id month sum]
p values.map {|arr| keys.zip(arr).to_h }
# => [ { "sales_user_id" => 8765, "user_id" => 105191, "month" => 2.0, "sum" => 1582.1 },
# { "sales_user_id" => 4321, "user_id" => 62870, "month" => 2.0, "sum" => 603.24 },
# { "sales_user_id" => 1234, "user_id" => 62870, "month" => 2.0, "sum" => 500.24 } ]
答案 1 :(得分:0)
arr = [[8765, 105191, 2.0, 1582.1 ],
[4321, 62870, 2.0, 603.24],
[1234, 62870, 2.0, 500.24]]
keys = ["sales_user_id", "user_id", "month", "sum"]
第一种方式
[keys].product(arr).map { |a| a.transpose.to_h }
#=> [{"sales_user_id"=>8765, "user_id"=>105191, "month"=>2.0, "sum"=>1582.1},
# {"sales_user_id"=>4321, "user_id"=> 62870, "month"=>2.0, "sum"=>603.24},
# {"sales_user_id"=>1234, "user_id"=> 62870, "month"=>2.0, "sum"=>500.24}]
步骤:
enum = [keys].product(arr)
#=> #<Enumerator:
# [
# [["sales_user_id", "user_id", "month", "sum"], [8765, 105191, 2.0, 1582.1]],
# [["sales_user_id", "user_id", "month", "sum"], [4321, 62870, 2.0, 603.24]],
# [["sales_user_id", "user_id", "month", "sum"], [1234, 62870, 2.0, 500.24]]
# ]:map>
a = enum.next
#=> [["sales_user_id", "user_id", "month", "sum"], [8765, 105191, 2.0, 1582.1]]
c = a.transpose
#=> [["sales_user_id", 8765], ["user_id", 105191], ["month", 2.0], ["sum", 1582.1]]
c.to_h
#=> {"sales_user_id"=>8765, "user_id"=>105191, "month"=>2.0, "sum"=>1582.1}
a = enum.next
#=> [["sales_user_id", "user_id", "month", "sum"], [4321, 62870, 2.0, 603.24]]
c = a.transpose
#=> [["sales_user_id", 4321], ["user_id", 62870], ["month", 2.0], ["sum", 603.24]]
c.to_h
#=> {"sales_user_id"=>4321, "user_id"=>62870, "month"=>2.0, "sum"=>603.24}
a = enum.next
#=> [["sales_user_id", "user_id", "month", "sum"], [1234, 62870, 2.0, 500.24]]
c = a.transpose
#=> [["sales_user_id", 1234], ["user_id", 62870], ["month", 2.0], ["sum", 500.24]]
c.to_h
#=> {"sales_user_id"=>1234, "user_id"=>62870, "month"=>2.0, "sum"=>500.24}
第二种方式
arr.map do |a|
ad = a.dup
{ "sales_user_id"=>ad.shift, "user_id"=>ad.shift, "month"=>ad.shift, "sum"=>ad.shift }
end
#=> [{"sales_user_id"=>8765, "user_id"=>105191, "month"=>2.0, "sum"=>1582.1},
# {"sales_user_id"=>4321, "user_id"=> 62870, "month"=>2.0, "sum"=>603.24},
# {"sales_user_id"=>1234, "user_id"=> 62870, "month"=>2.0, "sum"=>500.24}]
答案 2 :(得分:0)
如果每个对象都有一组固定值,也可以考虑使用Struct。几乎可以用作哈希。
User = Struct.new(:sales_user_id, :user_id, :month, :sum)
array = [[8765, 105191, 2.0, 1582.1], [4321, 62870, 2.0, 603.24], [1234, 62870, 2.0, 500.24]]
users = array.map {|values| User.new *values}
p users #=> [#<struct User sales_user_id=8765, user_id=105191, month=2.0, sum=1582.1>, #<struct User sales_user_id=4321, user_id=62870, month=2.0, sum=603.24>, #<struct User sales_user_id=1234, user_id=62870, month=2.0, sum=500.24>]
p users.first.user_id #=> 105191
p users.first['user_id'] #=> 105191
p users.first[:user_id] #=> 105191
答案 3 :(得分:0)
这是一种方法,使用.collect
迭代外部数组并为每个内部数组构造一个哈希:
values = [[8765, 105191, 2.0, 1582.1],
[4321, 62870, 2.0, 603.24],
[1234, 62870, 2.0, 500.24]]
result = values.collect do |array|
{
"sales_user_id" => array[0].to_s,
"user_id" => array[1].to_s,
"month" => array[2].to_i.to_s,
"sum" => array[3].to_s
}
end
result
:
[{"sales_user_id"=>"8765", "user_id"=>"105191", "month"=>"2", "sum"=>"1582.1"},
{"sales_user_id"=>"4321", "user_id"=>"62870", "month"=>"2", "sum"=>"603.24"},
{"sales_user_id"=>"1234", "user_id"=>"62870", "month"=>"2", "sum"=>"500.24"}]
它还使用.to_s
和.to_i
将结果强制转换为问题中的值(例如8765
到"8765"
和2.0
到{{1 }})。