首页 > 解决方案 > 如何找到列表中元素的所有索引?

问题描述

假设数组中有几个元素是重复的。

数组 = [1,2,4,6,1,6,9,12]

我需要找到 1 的所有索引,这意味着 0 和 4。谁能告诉我该怎么做?

标签: arraysvbscript

解决方案


下面将一个值数组作为第一个参数,将要查找的值作为第二个参数,并返回找到该值的索引数组:

Function FindIndexesOfElement(valuesArray, elmToFind) 
  Set indexes = CreateObject("Scripting.Dictionary") 

  For i = LBound(valuesArray) to UBound(valuesArray)
    If elmToFind = valuesArray(i) Then
      indexes(i) = True 
    End If
  Next 

  FindIndexesOfElement = indexes.Keys 
End Function

推荐阅读