[英]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.