查找包含任意数量的嵌套哈希和数组的哈希深处的键/值对
Web 服务返回的哈希值包含未知数量的嵌套哈希值,其中一些包含数组,而数组又包含未知数量的嵌套哈希值。
有些键不是唯一的——即存在于多个嵌套散列中。
然而,我真正关心的所有键都是唯一的。
有什么办法可以给顶级哈希提供一个键,并取回它的值,即使键值对深埋在这个泥沼中?
(该网络服务是亚马逊产品广告 API,它根据结果数量和每个产品类别允许的搜索类型稍微改变其提供的结果的结构。)
A web service is returning a hash that contains an unknown number of nested hashes, some of which contain an array, which in turn contains an unknown number of nested hashes.
Some of the keys are not unique -- i.e. are present in more than one of the nested hashes.
However, all the keys that I actually care about are all unique.
Is there someway I can give a key to the top-level hash, and get back it's value even if the key-value pair is buried deep in this morass?
(The web service is Amazon Product Advertising API, which slightly varies the structure of the results that it gives depending on the number of results and the search types permitted in each product category.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是一个简单的递归解决方案:
Here's a simple recursive solution:
无需猴子修补,只需使用 Hashie gem: https://github.com/intridea/hashie#deepfind< /a>
对于任意 Enumerable 对象,还有另一个可用的扩展,DeepLocate: https://github.com/intridea/hashie#deeplocate
No need for monkey patching, just use Hashie gem: https://github.com/intridea/hashie#deepfind
For arbitrary Enumerable objects, there is another extension available, DeepLocate: https://github.com/intridea/hashie#deeplocate
结合上面的一些答案和评论:
Combining a few of the answers and comments above:
Ruby 2.3 引入了 Hash#dig,它允许你要做的:
Ruby 2.3 introduces Hash#dig, which allows you to do:
几乎不知道的解决方案的变体:这将找到哈希中某个键的所有值,而不是第一个匹配项。
{a: [{b: 1}, {b: 2}]}.deep_find(:b)
将返回[1, 2]
A variation of barelyknown's solution: This will find all the values for a key in a hash rather than the first match.
{a: [{b: 1}, {b: 2}]}.deep_find(:b)
will return[1, 2]
尽管这似乎是一个常见问题,但我只是花了一段时间试图找到/提出我所需要的东西,我认为这与您的要求相同。第一个响应中的两个链接都不是正确的。
因此给出:
以下:
将找到与 :transaction 键关联的数组。
这不是最佳选择,因为即使填充了 memo,注入也会继续迭代。
Despite this appearing to be a common problem, I've just spent a while trying to find/come up with exactly what I need, which I think is the same as your requirement. Neither of the links in the first response are spot-on.
So given:
The following:
will find the array associated with the :transaction key.
This is not optimal as the inject will continue to iterate even if memo is populated.
我使用下面的代码
I use the following code
我最终使用它进行了一个小的特里搜索,我写道:
注意:这仅适用于目前的嵌套哈希。目前不支持数组。
I ended up using this for a small trie search I wrote:
Note: this is just for nested hashes at the moment. Currently no array support.
因为 Rails 5 ActionController::Parameters 不再继承自 Hash,所以我必须修改该方法并使其特定于参数。
如果找到该键,它会返回该键的值,但不会返回 ActionController::Parameter 对象,因此不会保留强参数。
Because Rails 5 ActionController::Parameters no longer inherits from Hash, I've had to modify the method and make it specific to parameters.
If the key is found, it returns the value of that key, but it doesn't return an ActionController::Parameter object so Strong Parameters are not preserved.