在嵌套哈希中查找多个对象

时间:2014-12-11 17:51:19

标签: ruby-on-rails ruby arrays hash

我正在从Facebook获得嵌套哈希。它的阵列里面有更多的哈希值,反之亦然。

现在我试图找到深度嵌套的哈希的每个出现。

在我的情况下,我正在搜索具有键“tags”的每个值,然后将其推送到数组中。

我找到了“Find key/value pairs deep inside a hash containing an arbitrary number of nested hashes and arrays”中唯一键的解决方案。

当密钥不唯一时,如何将每个结果推送到数组中,我该如何做同样的事情?

修改

以下是我从facebook图表api获得的示例回复。

{"albums"=>{"data"=>[{"id"=>"1406826642942218", 
"created_time"=>"2014-12-11T10:54:13+0000", 
"photos"=>{"data"=>[{"created_time"=>"2014-12-11T10:54:13+0000",
 "id"=>"1406826626275553"}], 
"paging"=>{"cursors"=>{"before"=>"MTQwNjgyNjYyNjI3NTU1Mw==", "after"=>"MTQwNjgyNjYyNjI3NTU1Mw=="}}}},
 {"id"=>"1406825849608964", 
"created_time"=>"2014-12-11T10:52:34+0000", 
"photos"=>{"data"=>[{"created_time"=>"2014-12-11T10:52:34+0000", 
"id"=>"1406825782942304"}], 
"paging"=>{"cursors"=>{"before"=>"MTQwNjgyNTc4Mjk0MjMwNA==", "after"=>"MTQwNjgyNTc4Mjk0MjMwNA=="}}}},
 {"id"=>"1405097859781763",
 "created_time"=>"2014-12-08T14:50:51+0000"
, "photos"=>{"data"=>[{"created_time"=>"2014-12-08T14:51:12+0000", "id"=>"1405097983115084"}],
 "paging"=>{"cursors"=>{"before"=>"MTQwNTA5Nzk4MzExNTA4NA==", "after"=>"MTQwNTA5Nzk4MzExNTA4NA=="}}}},
 {"id"=>"1392256877732528",
 "created_time"=>"2014-11-17T14:37:00+0000",
 "photos"=>{"data"=>[{"created_time"=>"2014-12-08T14:24:41+0000", 
"id"=>"1405084986449717"}, 
{"tags"=>{"data"=>[{"id"=>"100007516267052",
 "name"=>"Dorothy Amgeafbfgjeb Qinman",
 "created_time"=>"2014-11-17T14:37:12+0000",
 "x"=>46.604215456674, "y"=>72.330827067669}, 
{"id"=>"100007456544855", 
"name"=>"Richard Amgdefeddhee Carrierosen",
 "created_time"=>"2014-11-17T14:37:11+0000",
 "x"=>45.433255269321, "y"=>48.571428571429}, 
{"id"=>"100008446877693", 
"name"=>"Karen Amhddfhggfic Chaison",
 "created_time"=>"2014-11-17T14:37:09+0000",
 "x"=>49.414519906323, "y"=>31.578947368421}],
 "paging"=>{"cursors"=>{"before"=>"MTAwMDA3NTE2MjY3MDUy", "after"=>"MTAwMDA4NDQ2ODc3Njkz"}}},
 "created_time"=>"2014-11-17T14:37:01+0000",
 "id"=>"1392256757732540"}], 
"paging"=>{"cursors"=>{"before"=>"MTQwNTA4NDk4NjQ0OTcxNw==", "after"=>"MTM5MjI1Njc1NzczMjU0MA=="}}}}],
 "paging"=>{"cursors"=>{"after"=>"MTM5MjI1Njg3NzczMjUyOA==", "before"=>"MTQwNjgyNjY0Mjk0MjIxOA=="}}},
 "id"=>"100008446877693"}

我想得到的是这部分

{"tags"=>{"data"=>[{"id"=>"100007516267052",
     "name"=>"Dorothy Amgeafbfgjeb Qinman",
     "created_time"=>"2014-11-17T14:37:12+0000",
     "x"=>46.604215456674, "y"=>72.330827067669}, 
    {"id"=>"100007456544855", 
    "name"=>"Richard Amgdefeddhee Carrierosen",
     "created_time"=>"2014-11-17T14:37:11+0000",
     "x"=>45.433255269321, "y"=>48.571428571429}, 
    {"id"=>"100008446877693", 
    "name"=>"Karen Amhddfhggfic Chaison",
     "created_time"=>"2014-11-17T14:37:09+0000",
     "x"=>49.414519906323, "y"=>31.578947368421}],

可能在多个地方,具体取决于有多少张照片上有标签。

我希望这更清楚。

1 个答案:

答案 0 :(得分:1)

def nested_hash_values(obj,key)
  r = []  
  if obj.is_a?(Hash)        
    r.push(obj[key]) if obj.key?(key) 
    obj.each_value { |e| r += nested_hash_values(e,key) }
  end
  if obj.is_a?(Array)
    obj.each { |e| r += nested_hash_values(e,key) }
  end
  r
end

a = {"foo"=>["bar", "x", {"bar"=>["hello", {"foo"=>"world"}, "world!"], "foo"=>"BAR!"}, "enough?"], "bar"=>"foo"}
nested_hash_values(a, "foo")
=> [["bar", "x", {"bar"=>["hello", {"foo"=>"world"}, "world!"], "foo"=>"BAR!"}, "enough?"], "BAR!", "world"]

应返回一个数组,其中包含为给定键找到的所有值。

您可以在每个块中添加额外的if e.is_a?(Array) || e.is_a?(Hash)。这样可以避免不必要的方法调用,并将函数加速一点,但添加额外的代码。