flatMapIndexed

Common
JVM
JS
Native
1.4
@JvmName("flatMapIndexedIterable") inline fun <T, R> Array<out T>.flatMapIndexed(
    transform: (index: Int, T) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> ByteArray.flatMapIndexed(
    transform: (index: Int, Byte) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> ShortArray.flatMapIndexed(
    transform: (index: Int, Short) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> IntArray.flatMapIndexed(
    transform: (index: Int, Int) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> LongArray.flatMapIndexed(
    transform: (index: Int, Long) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> FloatArray.flatMapIndexed(
    transform: (index: Int, Float) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> DoubleArray.flatMapIndexed(
    transform: (index: Int, Double) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> BooleanArray.flatMapIndexed(
    transform: (index: Int, Boolean) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> CharArray.flatMapIndexed(
    transform: (index: Int, Char) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedSequence") inline fun <T, R> Array<out T>.flatMapIndexed(
    transform: (index: Int, T) -> Sequence<R>
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UIntArray.flatMapIndexed(
    transform: (index: Int, UInt) -> Iterable<R>
): List<R>
@ExperimentalUnsignedTypes inline fun <R> ULongArray.flatMapIndexed(
    transform: (index: Int, ULong) -> Iterable<R>
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UByteArray.flatMapIndexed(
    transform: (index: Int, UByte) -> Iterable<R>
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UShortArray.flatMapIndexed(
    transform: (index: Int, UShort) -> Iterable<R>
): List<R>

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.

import kotlin.test.*

fun main(args: Array<String>) {
//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
}
Common
JVM
JS
Native
1.4
@JvmName("flatMapIndexedIterable") inline fun <T, R> Iterable<T>.flatMapIndexed(
    transform: (index: Int, T) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedSequence") inline fun <T, R> Iterable<T>.flatMapIndexed(
    transform: (index: Int, T) -> Sequence<R>
): List<R>

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.

import kotlin.test.*

fun main(args: Array<String>) {
//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
}