【问题标题】:Find where int or string matches in array在数组中查找 int 或 string 匹配的位置
【发布时间】:2017-07-11 14:27:56
【问题描述】:

有什么方法可以让我不仅可以找出数组是否包含某些东西,还可以找出它在数组中的什么位置?

例如

[1,4,6,4,1] 中找到1 将返回[0,4]

【问题讨论】:

    标签: arrays ruby search


    【解决方案1】:

    您可以将each_indexselect 一起使用:

    arr = [1,4,6,4,1]
    arr.each_index.select { |i| arr[i] == 1 }
    #=> [0, 4]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 2020-02-20
      • 2013-09-16
      • 1970-01-01
      • 2022-01-21
      • 2011-07-24
      • 2013-08-10
      相关资源
      最近更新 更多