加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 访问由键数组指定的嵌套哈希元素

发布时间:2020-12-17 04:01:16 所属栏目:百科 来源:网络整理
导读:参见英文答案 ruby use array tvalues to index nested hash of hash ????????????????????????????????????4个 ???????????? Map array of ints to nested array access????????????????????????????????????2个 我试图得到一个通用的解决方案来解决在给定
参见英文答案 > ruby use array tvalues to index nested hash of hash ????????????????????????????????????4个
>???????????? Map array of ints to nested array access????????????????????????????????????2个
我试图得到一个通用的解决方案来解决在给定一个键值数组的情况下访问嵌套哈希中的元素的问题,例如:

hash = { "a" => { "b" => 'foo' }}
array = ["a","b"]

function(array)
=> "foo"

我猜这可能是一个单行.它也与这个问题密切相关:
Ruby convert array to nested hash

解决方法

hash = { "a" => { "b" => 'foo' }}
array = ["a","b"]

array.inject(hash,:fetch)
# => "foo"
array.inject(hash,:[])
# => "foo"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读