如何将哈希值转换为数组、状态和首字母缩略词?

发布于 2024-12-08 10:10:40 字数 1548 浏览 1 评论 0原文

Array = [{:acronym => "AC", :fullname => "Acre"}, {:acronym => "AL", :fullname => "Alagoas"}, {:acronym => "AP", :fullname => "Amapá"}, {:acronym => "AM", :fullname => "Amazonas"}, {:acronym => "BA", :fullname => "Bahia"}, {:acronym => "CE", :fullname => "Ceará"}, {:acronym => "DF", :fullname => "Distrito Federal"}, {:acronym => "ES", :fullname => "Espírito Santo"}, {:acronym => "GO", :fullname => "Goiás"}, {:acronym => "MA", :fullname => "Maranhão"}, {:acronym => "MT", :fullname => "Mato Grosso"}, {:acronym => "MS", :fullname => "Mato Grosso do Sul"}, {:acronym => "MG", :fullname => "Minas Gerais"}, {:acronym => "PA", :fullname => "Pará"}, {:acronym => "PB", :fullname => "Paraíba"}, {:acronym => "PR", :fullname => "Paraná"}, {:acronym => "PE", :fullname => "Pernambuco"}, {:acronym => "PI", :fullname => "Piauí"}, {:acronym => "RR", :fullname => "Roraima"}, {:acronym => "RO", :fullname => "Rondônia"}, {:acronym => "RJ", :fullname => "Rio de Janeiro"}, {:acronym => "RN", :fullname => "Rio Grande do Norte"}, {:acronym => "RS", :fullname => "Rio Grande do Sul"}, {:acronym => "SC", :fullname => "Santa Catarina"}, {:acronym => "SP", :fullname => "São Paulo"}, {:acronym => "SE", :fullname => "Sergipe"}, {:acronym => "TO", :fullname => "Tocantins"}]

如何将变量与 :acronym 进行比较并返回其他变量中的 :fullname ? 我正在尝试使用 Rails 助手来完成此操作。

Array = [{:acronym => "AC", :fullname => "Acre"}, {:acronym => "AL", :fullname => "Alagoas"}, {:acronym => "AP", :fullname => "Amapá"}, {:acronym => "AM", :fullname => "Amazonas"}, {:acronym => "BA", :fullname => "Bahia"}, {:acronym => "CE", :fullname => "Ceará"}, {:acronym => "DF", :fullname => "Distrito Federal"}, {:acronym => "ES", :fullname => "Espírito Santo"}, {:acronym => "GO", :fullname => "Goiás"}, {:acronym => "MA", :fullname => "Maranhão"}, {:acronym => "MT", :fullname => "Mato Grosso"}, {:acronym => "MS", :fullname => "Mato Grosso do Sul"}, {:acronym => "MG", :fullname => "Minas Gerais"}, {:acronym => "PA", :fullname => "Pará"}, {:acronym => "PB", :fullname => "Paraíba"}, {:acronym => "PR", :fullname => "Paraná"}, {:acronym => "PE", :fullname => "Pernambuco"}, {:acronym => "PI", :fullname => "Piauí"}, {:acronym => "RR", :fullname => "Roraima"}, {:acronym => "RO", :fullname => "Rondônia"}, {:acronym => "RJ", :fullname => "Rio de Janeiro"}, {:acronym => "RN", :fullname => "Rio Grande do Norte"}, {:acronym => "RS", :fullname => "Rio Grande do Sul"}, {:acronym => "SC", :fullname => "Santa Catarina"}, {:acronym => "SP", :fullname => "São Paulo"}, {:acronym => "SE", :fullname => "Sergipe"}, {:acronym => "TO", :fullname => "Tocantins"}]

How can I compare a variable with :acronym and return the :fullname in other variable?
I'm trying to do this using a Rails helper.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

猫弦 2024-12-15 10:10:40

首先,您不想调用数组 Array 因为这是一个类名并且它已经被占用。

你可以这样做:

def find_acronym_in(array, acronym)
    # returns nil if we don't find a match.
    array.find { |h| h[:acronym] == acronym }.try(:fetch, :fullname)
end

在你的 ERB 中:

<%= find_acronym_in(@acronyms, 'ES') %>

这是一个简单的线性搜索,因此对于大列表来说它会很慢,但对于短列表来说它可能不会引人注目。

如果你做了很多这样的事情或者如果你有很大的列表,那么你可以将你的数组重新排列成控制器中的一个简单的哈希:

@acronyms_hash = @acronyms.each_with_object({ }) { |h, memo| memo[h[:acronym]] = h[:fullname] }

然后根据需要从哈希中提取内容:

<%= @acronyms_hash['ES'] %>

First of all you don't want to call your array Array as that's a class name and it is already taken.

You could do this:

def find_acronym_in(array, acronym)
    # returns nil if we don't find a match.
    array.find { |h| h[:acronym] == acronym }.try(:fetch, :fullname)
end

and in your ERB:

<%= find_acronym_in(@acronyms, 'ES') %>

That's a simple linear search so it will be slow for large lists but it probably won't be noticeable for short lists.

If you're doing a lot of this or if you have large lists then you could rearrange your array into a simple Hash in your controller:

@acronyms_hash = @acronyms.each_with_object({ }) { |h, memo| memo[h[:acronym]] = h[:fullname] }

and then just pull things out of the Hash as needed:

<%= @acronyms_hash['ES'] %>
你是年少的欢喜 2024-12-15 10:10:40

这样就可以了。

acronym_to_find = "SE"
Array.select { |ac| ac[:acronym] == acronym_to_find }.first[:fullname]

您的数据可能会更好地构造为 { acronym =>; 的哈希值。不过,全名}对。

This will do it

acronym_to_find = "SE"
Array.select { |ac| ac[:acronym] == acronym_to_find }.first[:fullname]

Your data would probably be better structured as a hash of { acronym => fullname} pairs, though.

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