I have an array that has X
number of values in it. The following array only has 4, but I need the code to be dynamic and not reliant on only having four array objects.
我有一个数组中有X个值的数组。以下数组只有4个,但我需要代码是动态的,而不是只依赖于有四个数组对象。
array = ["Adult", "Family", "Single", "Child"]
array = [“Adult”,“Family”,“Single”,“Child”]
I want to convert array
to a hash that looks like this:
我想将数组转换为如下所示的哈希:
hash = {0 => 'Adult', 1 => 'Family', 2 => 'Single', 3 => 'Child'}
hash = {0 =>'Adult',1 =>'Family',2 =>'Single',3 =>'Child'}
The hash should have as many key/value pairs as the array has objects, and the values should start at 0 and increment by 1 for each object.
散列应该具有与数组具有对象一样多的键/值对,并且值应该从0开始并且对于每个对象递增1。
4 个解决方案
#1
15
Using Enumerable#each_with_index
:
使用Enumerable#each_with_index:
Hash[array.each_with_index.map { |value, index| [index, value] }]
# => {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
As @hirolau commented, each_with_index.map
can also be written as map.with_index
.
正如@hirolau评论的那样,each_with_index.map也可以写成map.with_index。
Hash[array.map.with_index { |value, index| [index, value] }]
# => {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
UPDATE
UPDATE
Alterantive that use Hash#invert
:
使用Hash #vert的Alterantive:
Hash[array.map.with_index{|*x|x}].invert
# => {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
Hash[[*array.map.with_index]].invert
# => {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
#2
4
Another one:
另一个:
Hash[array.each_index.zip(array)]
#=> {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
Newer Ruby versions would allow:
较新的Ruby版本允许:
array.each_with_index.to_h.invert
#=> {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
#3
1
Hash[*(0..array.size-1).to_a.zip(array)]
=> {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
#4
0
Try this
尝试这个
array.each_with_index.inject({}){ |hash, (val, i)| hash[i]=val; hash }
=> {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
#1
15
Using Enumerable#each_with_index
:
使用Enumerable#each_with_index:
Hash[array.each_with_index.map { |value, index| [index, value] }]
# => {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
As @hirolau commented, each_with_index.map
can also be written as map.with_index
.
正如@hirolau评论的那样,each_with_index.map也可以写成map.with_index。
Hash[array.map.with_index { |value, index| [index, value] }]
# => {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
UPDATE
UPDATE
Alterantive that use Hash#invert
:
使用Hash #vert的Alterantive:
Hash[array.map.with_index{|*x|x}].invert
# => {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
Hash[[*array.map.with_index]].invert
# => {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
#2
4
Another one:
另一个:
Hash[array.each_index.zip(array)]
#=> {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
Newer Ruby versions would allow:
较新的Ruby版本允许:
array.each_with_index.to_h.invert
#=> {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
#3
1
Hash[*(0..array.size-1).to_a.zip(array)]
=> {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}
#4
0
Try this
尝试这个
array.each_with_index.inject({}){ |hash, (val, i)| hash[i]=val; hash }
=> {0=>"Adult", 1=>"Family", 2=>"Single", 3=>"Child"}