如何访问嵌套元素,传球达阵坐标 [英] How to access a nested element, passing array with coordinates
本文介绍了如何访问嵌套元素,传球达阵坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有访问嵌套数组的元素任何一小段路,路过坐标数组?我的意思是这样的:
矩阵= [1,2,3,4],[5,6,7,8]
数组= [1,1]矩阵[排列]
#=> 6
我只是不知道是否有一个较短的版本比:
矩阵[阵列[0] [阵列[1]
解决方案
array.inject(矩阵:取)
#=> 6
Is there any short way to access an element of a nested array, passing the array with coordinates? I mean something like:
matrix = [[1,2,3,4],[5,6,7,8]]
array = [1,1]
matrix [array]
# => 6
I just wonder if there is a shorter version than:
matrix [array[0]][array[1]]
解决方案
array.inject(matrix, :fetch)
# => 6
这篇关于如何访问嵌套元素,传球达阵坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文