如何将多级数组中的唯一值映射到 value=>array 的哈希值?
我有一个看起来像这样的数组:
[[100, "one"],
[101, "one"],
[102, "one"],
[103, "two"],
[104, "three"],
[105, "three"]]
我想做的是创建一个看起来像这样的哈希数组
[{"one" => [100,101,102]},
{"two" => [103]},
{"three" => [104,105]}]
数字部分将始终是唯一的,字符串部分将有重复项。我想到这样做的每一种方式都会得到一些长函数,我想知道解决这个问题的“导轨方式”,我确信我缺少一些晦涩的函数。
I have an array that looks something like this:
[[100, "one"],
[101, "one"],
[102, "one"],
[103, "two"],
[104, "three"],
[105, "three"]]
What I would like to do is create an array of hashes that looks like this
[{"one" => [100,101,102]},
{"two" => [103]},
{"three" => [104,105]}]
The number portion will always be unique, the string portion will have duplicates. Every way I think about doing this I get some long function, I would like to know the "rails way" of going about this, I'm sure there's some obscure function I am missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不是 Rails 助手,而是常见的 Ruby 习惯用法可以帮助您实现这一目标。一点点
就可以了。
Not a Rails helper, but a common Ruby idiom can get you there. A little
will do the trick.
如果您需要的只是分组内容,那么您可以使用 Rails
group_by
函数:与您需要的相差不远。因此,如果您可以按原样使用它,我想那很好,但如果您需要精确的格式,您说的。我认为自己做比使用它和转换更容易。
If what you need is only group stuff then you can use the Rails
group_by
function :Not to far from what you need. So if you can use it as it stand, I guess that's fine, but if you need exaclty the format you said. I think it easier to do it yourself rather than usign this and converting.
没有真正的功能可以做到这一点。您需要使用注入并创建自己的哈希值。
There are no really function to do that. You need use inject and create yourself you hash.
正如 shingara 指出的那样,这对于数组的格式来说是非常特定的。您可以像这样做您需要的事情:
给出的结果如下:
{'one' => [101, 102], ... }
这比您对一键哈希数组的请求更好,IMO。As shingara points out, this is pretty specific to the format of the array(s). You can do what you need like so:
That gives a result like:
{'one' => [101, 102], ... }
which is better than your request for an array of one-key hashes, IMO.这是一个实现
Here's one implementation
您需要特定格式(即单元素哈希值数组而不仅仅是沼泽标准哈希值)是否有特定原因?因为在那种情况下,它实际上只是
Is there a specific reason why you need that specific format, i.e. an array of single-element hashes instead of just a bog-standard hash? Because in that case, it would literally be just
我能想到的准确获得您要求的最短方法是:
这可能就是您想要的。
请注意,
a
会因此被破坏,如果这让您烦恼,您可以
改为编写。
如果您确实想要您编写的格式,那么您可以添加另一个地图:
所以总结一下:
The shortest way to get exactly what you ask for that I can come up with is:
which is probably what you want.
please note that
a
gets ruined by thisif that bothers you, you can write
instead.
And if you really want that format you wrote then you can add another map:
So to sum up: