flatMapIndexed (original) (raw)
Returns a single list of all elements yielded from results of transform function being invoked on each element and its index in the original array.
Since Kotlin
1.4
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val data: List<String> = listOf("Abcd", "efgh", "Klmn")
val selected: List<Boolean> = data.map { it.any { c -> c.isUpperCase() } }
val result = data.flatMapIndexed { index, s -> if (selected[index]) s.toList() else emptyList() }
println(result) // [A, b, c, d, K, l, m, n]
//sampleEnd
}
Returns a single list of all elements yielded from results of transform function being invoked on each element and its index in the original collection.
Since Kotlin
1.4
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val data: List<String> = listOf("Abcd", "efgh", "Klmn")
val selected: List<Boolean> = data.map { it.any { c -> c.isUpperCase() } }
val result = data.flatMapIndexed { index, s -> if (selected[index]) s.toList() else emptyList() }
println(result) // [A, b, c, d, K, l, m, n]
//sampleEnd
}