将整数数组映射到嵌套数组访问 [英] Map array of ints to nested array access
本文介绍了将整数数组映射到嵌套数组访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法使用本身存储在数组中的索引来动态访问嵌套数组?
Is there a way to dynamically access a nested array using indices which are themselves stored in an array?
主数组/矩阵嵌套可以是可变的,例如2、4、100.
The main array/matrix nesting could be variable e.g. 2, 4, 100.
示例:
my_array = [
[[1, 2], [3, 4]],
[[5, 6], [7, 8]],
[[9, 10], [11, 12]]
]
my_array.access_using_array([0, 1, 1])
=> 4
推荐答案
[0, 1, 1].inject(my_array, :fetch)
# => 4
这篇关于将整数数组映射到嵌套数组访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文