开发者

find duplicates in each array rails

开发者 https://www.devze.com 2023-01-28 12:40 出处:网络
<% @sp_references.each do |sp开发者_如何学Python_ref| %> <% sp_ref.all_references.each_with_index do |tax_ref, i| %>

<% @sp_references.each do |sp开发者_如何学Python_ref| %>

<% sp_ref.all_references.each_with_index do |tax_ref, i| %> <%if (tax_ref.reference.uniq) && (tax_ref.reference !~ /emend$/i) %> <%= tax_ref.reference %> <%end%> <%end%> <%end%>


This 'uniq' option to get distinct elements in tax_ref.reference is not working. It shows "undefined method `uniq' for #


It seems to me like tax_ref.reference is not an array. uniq should be called on arrays. Try this instead:

<% sp_ref.all_references.keys.uniq.each do |tax_ref| %>
    <%if (tax_ref.reference !~ /emend$/sp_ref.all_references[tax_ref]) %>
        <%= tax_ref.reference %>
    <%end%>
<% end %>


uniq is a method on any enumerable object (arrays). So you can't call it on the reference object of tax_ref.

Instead, you'll want to call it on sp_ref.all_references, like sp_ref.all_references.uniq.each

This SO Question has some suggestions on filtering out duplicate objects when only an attribute is duplicated (not unique).


<%arr = Array.new %> <% @sp_references.each do |sp_ref| %> <% sp_ref.all_references.each_with_index do |tax_ref, i| %>  <%if (tax_ref.reference !~ /emend$/i) %>  <% arr.push("#{tax_ref.reference}") %> <%end%>  <%end%>  <%end%> <%= arr.uniq %>

It works for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消