jRuby / Rails 是否可以从哈希中获取唯一的项目并将唯一的项目组合在一起?
是否有可能获得哈希值的唯一值?假设我有一个散列,其中一个键是“名称”,名称可以出现几十次,但它只会有 10 个其自身唯一的变体。我想要做的是能够获取这 10 个唯一的名称并基于它们构建一个视图。但为了正确地推出视图,我需要知道这些唯一的名称。我想我必须构建一个数组,或者另一个散列或者一些唯一的名称。因此,我可以使用“each”遍历该数组,然后对于每个唯一的数组,如果原始哈希有匹配项,则将其应用于视图上的该部分或块。希望这对某人有意义。谁能帮帮我。
val = {
:status => "successful",
:service_list => [
{
:service_name => "oozie",
:status => "RUNNING",
:status_message => "Running Master Service",
:host => "1"
},
{
:service_name => "single-namenode",
:status => "RUNNING",
:status_message => "Running Service",
:host => "1"
},
{
:service_name => "single-database",
:status => "RUNNING",
:status_message => "Running Service",
:host => "1"
}
]}
我正在使用的哈希的截断版本。
Is it possible to get the Unique values of a Hash? Lets say I have a hash which one of the keys is "name" Name can come up a couple dozen times, but it will only have lets say 10 variations of itself all unique. What I want to do is be able to take those 10 unique names and build a view based off of them. But in order to roll the view out properly I would need to know those unique names. Im thinking I have to build an array, or another hash or something of the unique names. So I can go over that array with "each" then for each unique if the original hash has a match apply it to that section or block on the view. Hopefully this makes sense to someone. Who can help me out.
val = {
:status => "successful",
:service_list => [
{
:service_name => "oozie",
:status => "RUNNING",
:status_message => "Running Master Service",
:host => "1"
},
{
:service_name => "single-namenode",
:status => "RUNNING",
:status_message => "Running Service",
:host => "1"
},
{
:service_name => "single-database",
:status => "RUNNING",
:status_message => "Running Service",
:host => "1"
}
]}
truncated version of the hash I am working with.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定这是否是正确的答案,但是可以像这样获取唯一服务名称的列表:
更新:
迭代按 service_name 分组的服务的哈希更容易一些,这里有一个示例
:您的控制器的操作:
在您看来
希望有帮助
Not sure if it is the right answer, but the list of unique service names can be obtained like this:
Update:
Iterating over the hash of services grouped by service_name is a little easier, here's an example:
In the action of your controller:
In your view
Hope that helps