【问题标题】:How to get the Index of a specific item in Multidimensional (nested) Arrays?如何获取多维(嵌套)数组中特定项目的索引?
【发布时间】:2017-01-12 12:57:51
【问题描述】:
ary=[ [[0, 0], [0, 1], [0, 2]],
      [[0, 3], [0, 4], [0, 5]],
      [[0, 6], [0, 7], [0, 8]] ]

我正在尝试在“主”数组中查找索引,该数组包含一个带有[0, 4] 的数组,即1

我一直在研究这样的想法:

ary.each_index.select{|index| #(return index if [0,4] matches) }

【问题讨论】:

    标签: arrays ruby


    【解决方案1】:

    试试

    ary.find_index { |arr| arr.include?([0, 4]) }
    

    【讨论】:

    • 我不敢相信我之前没想到这个哈哈
    【解决方案2】:

    【讨论】:

    • 如果你不演示它是如何工作的,这并不是一个真正的答案。仅链接的答案被视为低质量。
    【解决方案3】:

    尝试关注。

    ary.find_index { |arr| arr.index([0, 4]) }
    

    【讨论】: