有没有一种简单的方法可以在数组中找到值的索引,例如使用index_of进行选择?

时间:2019-06-24 03:37:02

标签: ruby

如果我有一个重复值数组,并且需要查找所有出现的索引,例如

我尝试了each_with_index,但是需要像select这样更有效的东西

arr = [2,3,4,3,5,6,3,7,8]   
# Now, I'm trying to grab the indexes of all the occurrences of 3

    final=[]
    arr.each_with_index do |x, i|
      final << i if x == 3
    end

在决赛中,我正在收集3个位置的所有索引

最终数组应具有[1、3、6]

0 个答案:

没有答案
相关问题