循环遍历数组的最佳方式

时间:2022-08-25 19:59:27

My controller passes this array to the view:

我的控制器将此数组传递给视图:

@ipd = Ipdw.connection.select_all("select * from [My Preferences]")

This is my view code:

这是我的观看代码:

<h1>Hello, Rails</h1>
<body>

 <% @ipd.each do |ip| %>
 <p><%= ip %></p>
 <% end %>

</body>

This is displayed in the browser:

这将显示在浏览器中:

{"Pref ID"=>1, "Source ID"=>1, "Username"=>"RS2559", "Field Name"=>"CATEGORY", "String Value"=>"IP Placemat Deliverables", "Integer Value"=>nil, "Decimal Value"=>nil, "Created Dt Tm"=>2009-08-10 03:01:36 UTC, "Update Dt Tm"=>2009-12-14 16:04:01 UTC}

{"Pref ID"=>2, "Source ID"=>1, "Username"=>"RS2559", "Field Name"=>"TYPE", "String Value"=>nil, "Integer Value"=>nil, "Decimal Value"=>nil, "Created Dt Tm"=>2009-08-10 03:01:40 UTC, "Update Dt Tm"=>2009-12-14 16:04:01 UTC}

...

I want to show the "Username" and "Field Name" in a table. How can I loop through the array to show only these columns in a readable matter?

我想在表格中显示“用户名”和“字段名称”。如何循环遍历数组以在可读的内容中仅显示这些列?

3 个解决方案

#1


5  

It looks like your query is returning an array of hashes. Just use the keys to pick the values you are interested in:

看起来您的查询返回一个哈希数组。只需使用键选择您感兴趣的值:

<% @ipd.each do |ip| %>
  <p>
    <%= ip['Username'] %><br />
    <%= ip['Field Name'] %>
  </p>
<% end %>

#2


3  

How about something like:

怎么样的:

<h1>Hello, Rails</h1>
<body>

 <% @ipd.each do |ip| %>
  <p>
    <% ["Username", "Field Name"].each do |k| %>
      <%= ip[k] %>
    <% end %>
  </p>
 <% end %>

</body>

That, naturally, will just put the values on your page. You could do other things like build a table or make bullet points or whatever.

当然,这只会将值放在您的页面上。你可以做其他事情,比如建立一个桌子或制作子弹点等等。

That might look something like:

这可能看起来像:

<h1>Hello, Rails</h1>
<body>

  <table>
    <tr>
      <% @field_names.each do |field_name| %>
        <th>
          <%= field_name %>
        </th>
      <% end %>
    </tr>
    <% @ipd.each do |ip| %>
      <tr>
        <% @field_names.each do |field_name| %>
          <td>
            <%= ip[field_name] %>
          </td>
        <% end %>
      </tr>
    <% end %>
  </table>

</body>

This assumes in your controller you did something like:

这假设在你的控制器中你做了类似的事情:

@field_names = ["Username", "Field Name"]

That way, you can decide in your controller which fields you want (and in what order) and not have to futz with your html.erb file.

这样,你可以在你的控制器中决定你想要的字段(以及以什么顺序),而不必使用你的html.erb文件。

If you wanted to pretty up those label names a little, in your controller you could do:

如果你想在控制器中稍微提高一些标签名称,你可以这样做:

@label_mapping = {"Username"=>"User Name", "Field Name"=>"Field Name"}

And then:

<h1>Hello, Rails</h1>
<body>

  <table>
    <tr>
      <% @field_names.each do |field_name| %>
        <th>
          <%= label_mapping[field_name] %>
        </th>
      <% end %>
    </tr>
    <% @ipd.each do |ip| %>
      <tr>
        <% @field_names.each do |field_name| %>
          <td>
            <%= ip[field_name] %>
          </td>
        <% end %>
      </tr>
    <% end %>
  </table>

</body>

#3


2  

There are maybe 2 ways:

可能有两种方式:

First you have a model for this kind of data:

首先,您有这种数据的模型:

<% @ipd.each do |ip| %>
<p><%= ip.username %></p>
<% end %>

Secound: Your Dataset you get from your external Database looks like a hash. So try something like this:

Secound:您从外部数据库获得的数据集看起来像哈希。所以尝试这样的事情:

<% @ipd.each do |ip| %>
<p><%= ip["username"] %></p>
<% end %>

I think kind 2 is what you need ;).

我认为第2类是你需要的;)。

In a view i wouldn't use a shorter way of looping, be cause the code get less readable with something like this: abc.each{|letter| "<li>#{letter}</li>"}

在一个视图中,我不会使用更短的循环方式,因为代码变得不那么可读,如下所示:abc.each {| letter | “

  • #{信} ”}

  • #1


    5  

    It looks like your query is returning an array of hashes. Just use the keys to pick the values you are interested in:

    看起来您的查询返回一个哈希数组。只需使用键选择您感兴趣的值:

    <% @ipd.each do |ip| %>
      <p>
        <%= ip['Username'] %><br />
        <%= ip['Field Name'] %>
      </p>
    <% end %>
    

    #2


    3  

    How about something like:

    怎么样的:

    <h1>Hello, Rails</h1>
    <body>
    
     <% @ipd.each do |ip| %>
      <p>
        <% ["Username", "Field Name"].each do |k| %>
          <%= ip[k] %>
        <% end %>
      </p>
     <% end %>
    
    </body>
    

    That, naturally, will just put the values on your page. You could do other things like build a table or make bullet points or whatever.

    当然,这只会将值放在您的页面上。你可以做其他事情,比如建立一个桌子或制作子弹点等等。

    That might look something like:

    这可能看起来像:

    <h1>Hello, Rails</h1>
    <body>
    
      <table>
        <tr>
          <% @field_names.each do |field_name| %>
            <th>
              <%= field_name %>
            </th>
          <% end %>
        </tr>
        <% @ipd.each do |ip| %>
          <tr>
            <% @field_names.each do |field_name| %>
              <td>
                <%= ip[field_name] %>
              </td>
            <% end %>
          </tr>
        <% end %>
      </table>
    
    </body>
    

    This assumes in your controller you did something like:

    这假设在你的控制器中你做了类似的事情:

    @field_names = ["Username", "Field Name"]
    

    That way, you can decide in your controller which fields you want (and in what order) and not have to futz with your html.erb file.

    这样,你可以在你的控制器中决定你想要的字段(以及以什么顺序),而不必使用你的html.erb文件。

    If you wanted to pretty up those label names a little, in your controller you could do:

    如果你想在控制器中稍微提高一些标签名称,你可以这样做:

    @label_mapping = {"Username"=>"User Name", "Field Name"=>"Field Name"}
    

    And then:

    <h1>Hello, Rails</h1>
    <body>
    
      <table>
        <tr>
          <% @field_names.each do |field_name| %>
            <th>
              <%= label_mapping[field_name] %>
            </th>
          <% end %>
        </tr>
        <% @ipd.each do |ip| %>
          <tr>
            <% @field_names.each do |field_name| %>
              <td>
                <%= ip[field_name] %>
              </td>
            <% end %>
          </tr>
        <% end %>
      </table>
    
    </body>
    

    #3


    2  

    There are maybe 2 ways:

    可能有两种方式:

    First you have a model for this kind of data:

    首先,您有这种数据的模型:

    <% @ipd.each do |ip| %>
    <p><%= ip.username %></p>
    <% end %>
    

    Secound: Your Dataset you get from your external Database looks like a hash. So try something like this:

    Secound:您从外部数据库获得的数据集看起来像哈希。所以尝试这样的事情:

    <% @ipd.each do |ip| %>
    <p><%= ip["username"] %></p>
    <% end %>
    

    I think kind 2 is what you need ;).

    我认为第2类是你需要的;)。

    In a view i wouldn't use a shorter way of looping, be cause the code get less readable with something like this: abc.each{|letter| "<li>#{letter}</li>"}

    在一个视图中,我不会使用更短的循环方式,因为代码变得不那么可读,如下所示:abc.each {| letter | “

  • #{信} ”}