如何找到最后一个数组的键

时间:2018-12-28 09:15:49

标签: arrays ruby

我有一个具有数组值的哈希值:

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"]}

但是我找不到解决方案。有什么花招吗?

4 个答案:

答案 0 :(得分:4)

some_attributes['variants'].map{|a| a[-1][1]}
#=> ["XS", "S", "M", "L"]

第一个-1是第一个维度元素的最后一个索引。
第二个1只是第二维的第二个索引,
 -在这种情况下,实际上与另一个-1 /最后一个索引相同。

I.E。下面的效果是相同的:

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's Benchmark中进行检查:)

答案 1 :(得分:4)

看起来您可以将变体转换为哈希。

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"]

答案 2 :(得分:3)

只是出于好奇:

some_attributes['variants'].map { |(_, _), (_, _), (_, e)| e }
#⇒ ["XS", "S", "M", "L"]

some_attributes['variants'].map(&:flatten).map(&:last)
#⇒ ["XS", "S", "M", "L"]

答案 3 :(得分:3)

只是为了娱乐,其他选择:

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)