How to get the first index in which an element of an array satisfies a certain condition in Swift

2 Answers

0 votes
let arr = ["swift", "java", "python", "rust"]
 
let index = arr.firstIndex(where: {$0 == "java"})!
 
print(index)
 
 
 
 
 
 
/*
run:
 
1
 
*/

 



answered Jun 13, 2023 by avibootz
0 votes
let arr = [1, 4, 7, 8, 10, 19]
 
let index = arr.firstIndex(where: {$0 > 5})!
 
print(index)
 
 
 
 
 
 
/*
run:
 
2
 
*/

 



answered Jun 13, 2023 by avibootz
...