Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我有一个像下面这样的哈希数组(在 JSON 中)并且我想检查一个 Id 是否存在。
如果 Id 存在,那么我想返回货币列表(如果存在)(在 Id 19 的情况下可能不存在)。
我该怎么做呢 ?
"MyArray": [ { "Id": 14, "Currencies": { "48": 840, "410": 840, "978": 826 } }, { "Id": 19 } ]
a = [ { "Id" => 14, "Currencies" => { "48" => 840, "410" => 840, "978" => 826 } }, { "Id" => 19 } ] h = a.detect {|i| i["Id"] == 14 && i.has_key?("Currencies") } h["Currencies"].values unless h.nil? # => [840, 840, 826]