我正在尝试编写一些代码来转换此嵌套数组的元素:
array = [
[['number'], ['name'], ['postion'], ['points']],
[['91'], ['dave'], ['center'], ['42']],
[['82'], ['sanjay'], ['behind'], ['14']],
[['133'], ['ayman'], ['front'], ['23']]
]
到哈希:
user1 = {number: 91, name: dave, postion: center, points: 42}
user2 = {number: 82, name: Sanjay, ...}
如果有人能够轻松理解代码,那就太感激了。
答案 0 :(得分:3)
header, *data = array.map(&:flatten)
user1, user2, user3 = data.map { |row| header.zip(row).to_h }
答案 1 :(得分:2)
要使用任意数量的字段和用户将数据传输到示例输出,您可能会执行以下操作:
array = [
[['number'], ['name'], ['postion'], ['points']],
[ ['91'], ['dave'], ['center'], ['42']],
[ ['82'], ['sanjay'], ['behind'], ['14']],
[ ['133'], ['ayman'], ['front'], ['23']]]
temp, data={},{}
array.transpose.map { |(h, *rest)| temp[h]=rest.flatten }
(1..temp.max_by { |k,v| v.length }[1].length)
.each_with_index { |n, i| data["user%d" % [n]]=temp.map { |k,l| [k[0], l[i]] } }
data.map { |k, v| data[k]=v.to_h }
然后你得到:
> data
{"user1"=>{"number"=>"91", "name"=>"dave", "postion"=>"center", "points"=>"42"}, "user2"=>{"number"=>"82", "name"=>"sanjay", "postion"=>"behind", "points"=>"14"}, "user3"=>{"number"=>"133", "name"=>"ayman", "postion"=>"front", "points"=>"23"}}
答案 2 :(得分:2)
使用数组users
(而不是变量user1
,user2
等)更有意义,因此代码可以与任意数量的用户一起使用。然后,用户i
的值为user[i]
(从用户0
开始计算)。
keys, *values = array.map(&:flatten)
users = [keys.map(&:to_sym)].product(values).map { |pair| pair.transpose.to_h }
#=> [{:number=>"91", :name=>"dave", :postion=>"center", :points=>"42"},
# {:number=>"82", :name=>"sanjay", :postion=>"behind", :points=>"14"},
# {:number=>"133", :name=>"ayman", :postion=>"front", :points=>"23"}]
步骤如下。
keys, *values = array.map(&:flatten)
#=> [["number", "name", "postion", "points"],
# ["91", "dave", "center", "42"],
# ["82", "sanjay", "behind", "14"],
# ["133", "ayman", "front", "23"]]
keys
#=> ["number", "name", "postion", "points"]
values
#=> [["91", "dave", "center", "42"],
# ["82", "sanjay", "behind", "14"],
# ["133", "ayman", "front", "23"]]
a = keys.map(&:to_sym)
#=> [:number, :name, :postion, :points]
b = [a]
#=> [[:number, :name, :postion, :points]]
c = b.product(values)
#=> [[[:number, :name, :postion, :points], ["91", "dave", "center", "42"]],
[[:number, :name, :postion, :points], ["82", "sanjay", "behind", "14"]],
[[:number, :name, :postion, :points], ["133", "ayman", "front", "23"]]]
c.map { |pair| pair.transpose.to_h }
# <return value above>
执行最后一步时,c
的每个元素都传递给块,块变量pair
被赋值给该值,并执行块计算。当c
的第一个元素传递给块时,将执行以下步骤。
pair = c.first
#=> [[:number, :name, :postion, :points], ["91", "dave", "center", "42"]]
d = pair.transpose
#=> [[:number, "91"], [:name, "dave"], [:postion, "center"], [:points, "42"]]
d.to_h
#=> {:number=>"91", :name=>"dave", :postion=>"center", :points=>"42"}
其余的计算方法类似。
答案 3 :(得分:1)
您可以获取下一个哈希的每个键,将第一个数组放在主数组中,如:
array.first.flatten
# => ["number", "name", "postion", "points"]
那么,你可以得到数组的其余部分,从索引1开始到最后一个元素,&#34;展平&#34;它得到一个数组,每4个值,如:
p array[1..-1].flatten.each_slice(4).to_a
[["91", "dave", "center", "42"], ["82", "sanjay", "behind", "14"], ["133", "ayman", "front", "23"]]
那么你就可以为每个数组创建一个哈希值,每个数组中包含4个值,并使用keys数组中的每个值,如哈希键:
p array[1..-1].flatten.each_slice(4).map.with_index{|e,i|
Hash[e.map.with_index{|f,i| [keys[i], f]}]
}
# [
# {"number"=>"91", "name"=>"dave", "postion"=>"center", "points"=>"42"},
# {"number"=>"82", "name"=>"sanjay", "postion"=>"behind", "points"=>"14"},
# {"number"=>"133", "name"=>"ayman", "postion"=>"front", "points"=>"23"}
# ]
拥有它可以迭代它,或根据需要创建本地或实例变量。