IndexOf 函数

完全限定名称:Std.Arrays.IndexOf

function IndexOf<'T>(predicate : ('T -> Bool), array : 'T[]) : Int

总结

返回满足给定谓词的数组中第一个元素的第一个索引。 如果不存在此类元素,则返回 -1。

输入

谓语

一个谓词函数,作用于数组的元素。

数组

使用给定谓词搜索的数组。

输出

predicate(array[index]) 为 true 的元素的最小索引,或 -1 不存在此类元素。

以下代码获取输入数组中第一个偶数的索引。

let indexOfFirstEven = IndexOf(x -> x % 2 == 0, [1, 3, 17, 2, 21]);
// `indexOfFirstEven` is 3.