将内容从数组转换为哈希

时间:2018-03-16 18:40:24

标签: arrays ruby hash

我的安排如下:

data = [
  [{
    :devicetotal => "ACH-EPN-AN01",
    :porttotal => "to_BTS IP-CAC HUARAZ - TAI5670",
    :queueIdtotal => "3",
    :device_int_stats_total => "ACH-EPN-AN01to_BTS IP-CAC HUARAZ - TAI56703",
    :timeCapturedtotal => "March 15, 2018 at 07:06 PM",
    :discard_sub => 0
  }],
  [{
    :devicetotal => "ACH-EPN-AN01",
    :porttotal => "to_BTS IP-CAC HUARAZ - TAI5670",
    :queueIdtotal => "6",
    :device_int_stats_total => "ACH-EPN-AN01to_BTS IP-CAC HUARAZ - TAI56706",
    :timeCapturedtotal => "March 15, 2018 at 07:06 PM",
    :discard_sub => 0
  }]
]

我如何使用以下格式?

data = [{
  :devicetotal => "ACH-EPN-AN01",
  :porttotal => "to_BTS IP-CAC HUARAZ - TAI5670",
  :queueIdtotal => "3",
  :device_int_stats_total => "ACH-EPN-AN01to_BTS IP-CAC HUARAZ - TAI56703",
  :timeCapturedtotal => "March 15, 2018 at 07:06 PM",
  :discard_sub => 0
}, {
  :devicetotal => "ACH-EPN-AN01",
  :porttotal => "to_BTS IP-CAC HUARAZ - TAI5670",
  :queueIdtotal => "6",
  :device_int_stats_total => "ACH-EPN-AN01to_BTS IP-CAC HUARAZ - TAI56706",
  :timeCapturedtotal => "March 15, 2018 at 07:06 PM",
  :discard_sub => 0
}]

我使用了以下代码,但收到错误。

data2 = Array.new []
data.each do |traf_sub|
  data2 << traf_sub[1]
end

1 个答案:

答案 0 :(得分:0)

使用flatten

data.flatten
#=>  [{:devicetotal=>"ACH-EPN-AN01", :porttotal=>"to_BTS IP-CAC HUARAZ - TAI5670", :queueIdtotal=>"3", :device_int_stats_total=>"ACH-EPN-AN01to_BTS IP-CAC HUARAZ - TAI56703", :timeCapturedtotal=>"March 15, 2018 at 07:06 PM", :discard_sub=>0}, {:devicetotal=>"ACH-EPN-AN01", :porttotal=>"to_BTS IP-CAC HUARAZ - TAI5670", :queueIdtotal=>"6", :device_int_stats_total=>"ACH-EPN-AN01to_BTS IP-CAC HUARAZ - TAI56706", :timeCapturedtotal=>"March 15, 2018 at 07:06 PM", :discard_sub=>0}]