Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get single field value in object list [duplicate]

how to get some field list in object list in kotlin

fun main(args:Array<String>){
    println("Hello World")
    val list = listOf(member("hong",10), member("kil", 10))
}

data class member(var name:String, var age:Int)

Above code, I want to get single field list in above code such as name

  {"hong", "kil"}
like image 700
Taehyung Kim Avatar asked May 30 '18 11:05

Taehyung Kim


1 Answers

You want to get list with only name field? If so, then use map (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map.html)

val names = list.map { it.name }

like image 87
Mamykin Andrey Avatar answered Oct 24 '22 12:10

Mamykin Andrey