How do I find .index of a multidimensional array

后端 未结 7 1656
-上瘾入骨i
-上瘾入骨i 2021-02-08 19:34

Tried web resources and didnt have any luck and my visual quick start guide.

If I have my 2d/multidimensional array:

 array = [[\'x\', \'x\',\' x\',\'x\'         


        
7条回答
  •  生来不讨喜
    2021-02-08 20:35

    Specifies both indexes of the first occurrence of element for one pass on subarrays

    a = [[...],[...],[...],[...]]
    element = 'S'
    result_i = result_j = nil
    
    a.each_with_index do|row, i|
        if (j = row.index(element))
            result_i, result_j = i, j       
            break
        end
    end
    

提交回复
热议问题