简体   繁体   中英

How do I filter Nones out of a column of options?

I thought this would work to extract a List[String], but no. (anmKey is Option[String])

run(query[Anm].map(_.anmKey).flatMap(_))

To get rid of the options, and the nones, use flatten

scala> List(Some("string1"), None, Some("string2")).flatten
val res0: List[String] = List(string1, string2)

Alternatively, if you want to keep the options but remove the nones, you can use filter .

scala> List(Some("string1"), None, Some("string2")).filter(_.isDefined)
val res1: List[Option[String]] = List(Some(string1), Some(string2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM