jRuby / Rails 是否可以从哈希中获取唯一的项目并将唯一的项目组合在一起?

发布于 2024-12-20 21:05:33 字数 916 浏览 1 评论 0原文

是否有可能获得哈希值的唯一值?假设我有一个散列,其中一个键是“名称”,名称可以出现几十次,但它只会有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2024-12-27 21:05:33

不确定这是否是正确的答案,但是可以像这样获取唯一服务名称的列表:

val[:service_list].map { |service| service[:service_name] }.uniq
# ["oozie", "single-namenode", "single-database"]

更新

迭代按 service_name 分组的服务的哈希更容易一些,这里有一个示例

:您的控制器的操作:

@services =  val[:service_list].group_by { |service| service[:service_name] }

# {
#    "oozie" => [ { ... }, { ... } ],
#    "single-namenode" => [ { ... }, { ... } ],
#    "single-database" => [ { ... }, { ... } ]
# }

在您看来

<% for name, services in @services %>
    All services with name <%= name %>
    <% for service in services %>
        <%= service[:host] %>, Status: <%= service[:status] %>
    <% end %>
<% end %>

希望有帮助

Not sure if it is the right answer, but the list of unique service names can be obtained like this:

val[:service_list].map { |service| service[:service_name] }.uniq
# ["oozie", "single-namenode", "single-database"]

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:

@services =  val[:service_list].group_by { |service| service[:service_name] }

# {
#    "oozie" => [ { ... }, { ... } ],
#    "single-namenode" => [ { ... }, { ... } ],
#    "single-database" => [ { ... }, { ... } ]
# }

In your view

<% for name, services in @services %>
    All services with name <%= name %>
    <% for service in services %>
        <%= service[:host] %>, Status: <%= service[:status] %>
    <% end %>
<% end %>

Hope that helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文