I have an array of hashes:
我有一组散列:
[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}]
How can I convert it to array of values:
如何将其转换为值数组:
["male", "male", "female"]
6 个解决方案
#1
2
A generic approach to this that would take into account other possible keys:
为此采取的一种一般性办法是考虑到其他可能的要点:
list = [{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}]
# Collect the 'sex' key of each hash item in the list.
sexes = list.collect { |e| e['sex'] }
#2
4
In this case,
在这种情况下,
[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].map(&:values).flatten
should work.
应该工作。
It takes an array from each hash, then flatten the nested array.
它从每个散列中获取一个数组,然后将嵌套数组变平。
#3
4
[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].flat_map(&:values)
#4
2
arr = [{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}]
arr.map(&:values).flatten
EDIT: As directed by @tadman. Thanks!
编辑:由@tadman指导。谢谢!
#5
1
You can "map" the elements inside array, take the values of hashes, them "flatten" the resultant array.
您可以“映射”数组中的元素,取散列的值,它们“平坦化”结果数组。
[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].map{|h| h.values }
=> [["male"], ["male"], ["female"]]
[["male"], ["male"], ["female"]].flatten
=> ["male", "male", "female"]
In a single line, you can:
在一行中,你可以:
[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].map{|h| h.values }.flatten
or:
或者:
[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].map(&:values).flatten
Docs:
文档:
- http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-values
- http://www.ruby-doc.org/core-1.9.3/Hash.html method-i-values
- http://ruby-doc.org/core-1.9.3/Array.html#method-i-flatten
- http://ruby-doc.org/core-1.9.3/Array.html method-i-flatten
- http://ruby-doc.org/core-1.9.3/Array.html#method-i-map
- http://ruby-doc.org/core-1.9.3/Array.html method-i-map
#6
-2
arr = Array.new
my_hash.each do |el|
#collect them here...
arr.push(el["sex"]);
end
Hope it helps
希望它能帮助
#1
2
A generic approach to this that would take into account other possible keys:
为此采取的一种一般性办法是考虑到其他可能的要点:
list = [{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}]
# Collect the 'sex' key of each hash item in the list.
sexes = list.collect { |e| e['sex'] }
#2
4
In this case,
在这种情况下,
[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].map(&:values).flatten
should work.
应该工作。
It takes an array from each hash, then flatten the nested array.
它从每个散列中获取一个数组,然后将嵌套数组变平。
#3
4
[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].flat_map(&:values)
#4
2
arr = [{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}]
arr.map(&:values).flatten
EDIT: As directed by @tadman. Thanks!
编辑:由@tadman指导。谢谢!
#5
1
You can "map" the elements inside array, take the values of hashes, them "flatten" the resultant array.
您可以“映射”数组中的元素,取散列的值,它们“平坦化”结果数组。
[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].map{|h| h.values }
=> [["male"], ["male"], ["female"]]
[["male"], ["male"], ["female"]].flatten
=> ["male", "male", "female"]
In a single line, you can:
在一行中,你可以:
[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].map{|h| h.values }.flatten
or:
或者:
[{"sex"=>"male"}, {"sex"=>"male"}, {"sex"=>"female"}].map(&:values).flatten
Docs:
文档:
- http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-values
- http://www.ruby-doc.org/core-1.9.3/Hash.html method-i-values
- http://ruby-doc.org/core-1.9.3/Array.html#method-i-flatten
- http://ruby-doc.org/core-1.9.3/Array.html method-i-flatten
- http://ruby-doc.org/core-1.9.3/Array.html#method-i-map
- http://ruby-doc.org/core-1.9.3/Array.html method-i-map
#6
-2
arr = Array.new
my_hash.each do |el|
#collect them here...
arr.push(el["sex"]);
end
Hope it helps
希望它能帮助