循环嵌套在另一个数组中的哈希数组

时间:2016-06-05 17:40:38

标签: ruby hashmap

我在哈希中的数组内部有一个数组。内部数组是一个哈希数组。我无法弄清楚如何访问它们。

{"AssetWarrantyResponse"=>
    [{"AssetHeaderData"=>
        {"BUID"=>"11", "ServiceTag"=>"GH3RV12", "ShipDate"=>"2014-06-05T00:00:00", "CountryLookupCode"=>"US", "LocalChannel"=>"45", "CustomerNumber"=>"127963428", "ItemClassCode"=>"RK003", "IsDuplicate"=>false, "MachineDescription"=>"OptiPlex 3020", "OrderNumber"=>"631899941", "ParentServiceTag"=>nil},
      "ProductHeaderData"=>
        {"SystemDescription"=>"OptiPlex 3020", "ProductId"=>"optiplex-3020-desktop", "ProductFamily"=>"Desktops & All-in-Ones", "LOB"=>"OptiPlex", "LOBFriendlyName"=>"OptiPlex"}, 
     "AssetEntitlementData"=>[
        {"StartDate"=>"2022-06-07T00:00:00", "EndDate"=>"2022-06-08T00:00:00", "ServiceLevelDescription"=>"Dell", "ServiceLevelCode"=>"DL", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"999-0036"}, 
        {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-05T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"422-0052"}, 
        {"StartDate"=>"2015-06-06T00:00:00", "EndDate"=>"2017-06-05T23:59:59", "ServiceLevelDescription"=>"Next Business Day Onsite", "ServiceLevelCode"=>"ND", "ServiceLevelGroup"=>5, "EntitlementType"=>"EXTENDED", "ServiceProvider"=>"UNY", "ItemNumber"=>"939-7368"}, 
        {"StartDate"=>"2015-06-06T00:00:00", "EndDate"=>"2017-06-05T23:59:59", "ServiceLevelDescription"=>"ProSupport", "ServiceLevelCode"=>"TS", "ServiceLevelGroup"=>8, "EntitlementType"=>"EXTENDED", "ServiceProvider"=>"DELL", "ItemNumber"=>"939-7738"},
        {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2015-06-05T23:59:59", "ServiceLevelDescription"=>"Next Business Day Onsite", "ServiceLevelCode"=>"ND", "ServiceLevelGroup"=>5, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"UNY", "ItemNumber"=>"939-6888"}, 
        {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2015-06-05T23:59:59", "ServiceLevelDescription"=>"ProSupport", "ServiceLevelCode"=>"TS", "ServiceLevelGroup"=>8, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"939-7718"}, 
        {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-08T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"422-0008"}, 
        {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-08T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"421-9491"}, 
        {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-08T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"421-9982"}
    ]}
], 
"InvalidFormatAssets"=>{"BadAssets"=>[]}, "InvalidBILAssets"=>{"BadAssets"=>[]}, "ExcessTags"=>{"BadAssets"=>[]}, "AdditionalInformation"=>nil}

以下代码返回哈希的完整数组,而不是循环哈希。

@hash['AssetWarrantyResponse'][0]['AssetEntitlementData'].each do |k| k['StartDate'] end 

返回:

[{"StartDate"=>"2022-06-07T00:00:00", "EndDate"=>"2022-06-08T00:00:00", "ServiceLevelDescription"=>"Dell", "ServiceLevelCode"=>"DL", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"999-0036"}, {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-05T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"422-0052"}, {"StartDate"=>"2015-06-06T00:00:00", "EndDate"=>"2017-06-05T23:59:59", "ServiceLevelDescription"=>"Next Business Day Onsite", "ServiceLevelCode"=>"ND", "ServiceLevelGroup"=>5, "EntitlementType"=>"EXTENDED", "ServiceProvider"=>"UNY", "ItemNumber"=>"939-7368"}, {"StartDate"=>"2015-06-06T00:00:00", "EndDate"=>"2017-06-05T23:59:59", "ServiceLevelDescription"=>"ProSupport", "ServiceLevelCode"=>"TS", "ServiceLevelGroup"=>8, "EntitlementType"=>"EXTENDED", "ServiceProvider"=>"DELL", "ItemNumber"=>"939-7738"}, {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2015-06-05T23:59:59", "ServiceLevelDescription"=>"Next Business Day Onsite", "ServiceLevelCode"=>"ND", "ServiceLevelGroup"=>5, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"UNY", "ItemNumber"=>"939-6888"}, {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2015-06-05T23:59:59", "ServiceLevelDescription"=>"ProSupport", "ServiceLevelCode"=>"TS", "ServiceLevelGroup"=>8, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"939-7718"}, {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-08T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"422-0008"}, {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-08T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"421-9491"}, {"StartDate"=>"2014-06-05T00:00:00", "EndDate"=>"2022-06-08T23:59:59", "ServiceLevelDescription"=>"Dell Digitial Delivery", "ServiceLevelCode"=>"D", "ServiceLevelGroup"=>11, "EntitlementType"=>"INITIAL", "ServiceProvider"=>"DELL", "ItemNumber"=>"421-9982"}]

有人能指出我正确的方向吗?我想遍历每个哈希。

1 个答案:

答案 0 :(得分:1)

Array#each返回原始数组。在使用each上的块调用"AssetEntitlementData"时,您将在数组中的每个哈希上调用块,然后返回原始数组。如果您尝试返回"StartDate"值的数组,请尝试以下操作:

@hash['AssetWarrantyResponse'][0]['AssetEntitlementData'].map do |h| 
  h['StartDate']
end

Array#map将返回一个数组,其结果是在每个元素上调用块。

相关问题