[英]How to find the key of the last array
我有一个具有数组值的哈希:
some_attributes['variants']
# =>
# [
# [["variantCode", "0715839001002"], ["sizeCode", "002"], ["sizeName", "XS"]],
# [["variantCode", "0715839001003"], ["sizeCode", "003"], ["sizeName", "S"]],
# [["variantCode", "0715839001004"], ["sizeCode", "004"], ["sizeName", "M"]],
# [["variantCode", "0715839001005"], ["sizeCode", "005"], ["sizeName", "L"]]
# ]
我期待在一个新数组中每个“sizeName”的键:
['XS', 'S', 'M', 'L']
我试过这样:
some_attributes['variants'[[['sizeName']]]]
some_attributes['variants'].select{|size| sizeName["sizeName"]}
但我找不到解决方案。 有什么技巧吗?
some_attributes['variants'].map{|a| a[-1][1]}
#=> ["XS", "S", "M", "L"]
其中第一个-1
是第一个维度的最后一个元素的索引。
第二个1
只是第二个维度的第二个索引,
-- 在这种情况下,实际上与另一个-1
/ 最后一个索引相同。
下面的 IE 效果相同:
some_attributes['variants'].map{|a| a[-1][-1]}
#=> ["XS", "S", "M", "L"]
增加可读性:
some_attributes['variants'].map{|a| a.last.last}
#=> ["XS", "S", "M", "L"]
这种表示法不仅更直观,而且运行速度更快,请查看下面的iGian 基准测试:)
看起来您可以将变体转换为哈希值。
some_attributes = {
"variants" => [
[["variantCode", "0715839001002"], ["sizeCode", "002"], ["sizeName", "XS"]],
[["variantCode", "0715839001003"], ["sizeCode", "003"], ["sizeName", "S"]],
[["variantCode", "0715839001004"], ["sizeCode", "004"], ["sizeName", "M"]],
[["variantCode", "0715839001005"], ["sizeCode", "005"], ["sizeName", "L"]]
]
}
variants = some_attributes['variants'].map(&:to_h)
variants.map { |variant| variant['sizeName'] }
=> ["XS", "S", "M", "L"]
然后更容易执行以下操作:
large_variant = variants.find { |variant| variant['sizeName'] == 'L' }
puts large_variant['variantCode']
# outputs:
# 0715839001005
或者只是得到你想要的,简单地:
some_attributes['variants'].map { |a| a.last.last }
#=> ["XS", "S", "M", "L"]
只是出于好奇:
some_attributes['variants'].map { |(_, _), (_, _), (_, e)| e }
#⇒ ["XS", "S", "M", "L"]
some_attributes['variants'].map(&:flatten).map(&:last)
#⇒ ["XS", "S", "M", "L"]
只是为了好玩,其他选择:
some_attributes["variants"].map(&:last).map(&:last)
#=> ["XS", "S", "M", "L"]
还
some_attributes["variants"].transpose.last.transpose.last
#=> ["XS", "S", "M", "L"]
甚至是混合:
some_attributes["variants"].map(&:last).transpose.last
#=> ["XS", "S", "M", "L"]
require 'benchmark' n = 5000 Benchmark.bm do |x| x.report("tiw_____") { n.times { some_attributes['variants'].map{|a| a[-1][1]} } } x.report("kimmo___") { n.times { some_attributes['variants'].map { |a| a.last.last } } } x.report("Aleksei1") { n.times { some_attributes['variants'].map { |(_, _), (_, _), (_, e)| e } } } x.report("igian1__") { n.times { some_attributes["variants"].map(&:last).map(&:last) } } x.report("igian3__") { n.times { some_attributes["variants"].map(&:last).transpose.last } } x.report("igian2__") { n.times { some_attributes["variants"].transpose.last.transpose.last } } x.report("Aleksei2") { n.times { some_attributes['variants'].map(&:flatten).map(&:last) } } end
一个结果(每次运行结果都有一点变化):
# user system total real # tiw_____ 0.007577 0.000078 0.007655 ( 0.007709) # kimmo___ 0.003979 0.000086 0.004065 ( 0.004070) # Aleksei1 0.008227 0.000158 0.008385 ( 0.008542) # igian1__ 0.008080 0.000132 0.008212 ( 0.008220) # igian2__ 0.011956 0.000168 0.012124 ( 0.012571) # igian3__ 0.013975 0.000122 0.014097 ( 0.014261) # Aleksei2 0.054203 0.002921 0.057124 ( 0.059449)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.