如何从以值为数组的散列中获取值?

时间:2021-09-24 14:30:58

I have a hash:

我有一个散列:

a = {
  "person" => [
    {
      "age"     => ["0"],
      "details" => [
        {
          "hobby"    => ["6"],
          "interest" => ["programming"]
        }
      ]
    }
  ]
}

I would like to get the value of the "details". The easy way to do it will be

我想知道“细节”的价值。简单的方法是

a["person"].first["details"].first 

But, it is too long and doesn't look good. Is there any other way I can do this?

但是,它太长了,看起来不太好。我还有别的办法吗?

2 个解决方案

#1


3  

If your search conditions based on structure of hash, the easiest way to do it will be represent this structure in access command what you have done.

如果您的搜索条件基于散列结构,那么最简单的方法就是在访问命令中表示该结构。

If you want several chars less, here it is:

如果你想要少几个字符,这里是:

a["person"][0]["details"][0]

If your search conditions based on key names, you can create your own function in Hash class to search over nested hashes and do something like this:

如果您的搜索条件基于关键名称,您可以在Hash类中创建自己的函数来搜索嵌套的散列,并执行如下操作:

a.search(:details).first

If you don't like square brackets, use https://github.com/intridea/hashie and access data in hash through attributes:

如果您不喜欢方括号,可以使用https://github.com/persistdea/hashie并通过属性哈希访问数据:

a.person.first.details.first

Anyway, you can do nothing, because your code looks pretty much usual for any rubyist.

无论如何,你什么都做不了,因为你的代码对于任何一个rubyist来说都是很常见的。

#2


3  

The original code of getting value from the hash looks ok for me. If you want to do something nicer it's only to modify the hash:

从散列中获取值的原始代码对我来说还可以。如果你想做一些更好的事情,只需要修改散列:

a = {"person"=>{"age"=>["0"], "details"=>{"hobby"=>["6"], "interest"=>["programming"]}}}

Then you can get access in better way:

然后你可以用更好的方式进入:

a['person']['details'] #=> {"hobby"=>["6"], "interest"=>["programming"]}

#1


3  

If your search conditions based on structure of hash, the easiest way to do it will be represent this structure in access command what you have done.

如果您的搜索条件基于散列结构,那么最简单的方法就是在访问命令中表示该结构。

If you want several chars less, here it is:

如果你想要少几个字符,这里是:

a["person"][0]["details"][0]

If your search conditions based on key names, you can create your own function in Hash class to search over nested hashes and do something like this:

如果您的搜索条件基于关键名称,您可以在Hash类中创建自己的函数来搜索嵌套的散列,并执行如下操作:

a.search(:details).first

If you don't like square brackets, use https://github.com/intridea/hashie and access data in hash through attributes:

如果您不喜欢方括号,可以使用https://github.com/persistdea/hashie并通过属性哈希访问数据:

a.person.first.details.first

Anyway, you can do nothing, because your code looks pretty much usual for any rubyist.

无论如何,你什么都做不了,因为你的代码对于任何一个rubyist来说都是很常见的。

#2


3  

The original code of getting value from the hash looks ok for me. If you want to do something nicer it's only to modify the hash:

从散列中获取值的原始代码对我来说还可以。如果你想做一些更好的事情,只需要修改散列:

a = {"person"=>{"age"=>["0"], "details"=>{"hobby"=>["6"], "interest"=>["programming"]}}}

Then you can get access in better way:

然后你可以用更好的方式进入:

a['person']['details'] #=> {"hobby"=>["6"], "interest"=>["programming"]}