如何找到列表中元素的所有索引?
How to find all the indexes of the element in the list?
假设数组中有几个元素是重复的。
数组 = [1,2,4,6,1,6,9,12]
我需要找到 1 的所有索引,也就是 0 和 4。谁能告诉我怎么做?
以下将值数组作为第一个参数,要查找的值作为第二个参数,returns 找到值的索引数组:
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
假设数组中有几个元素是重复的。
数组 = [1,2,4,6,1,6,9,12]
我需要找到 1 的所有索引,也就是 0 和 4。谁能告诉我怎么做?
以下将值数组作为第一个参数,要查找的值作为第二个参数,returns 找到值的索引数组:
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