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

145 Views Asked by At

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

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

There are 1 best solutions below

2
On

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))