αƞjiβ αƞjiβ - 2 months ago 13
Scala Question

Break out of double foreach in Scala

I have to return true or false based on field value in inner set item. My loops is as follow

myChoice.category.foreach(category => {
category.flavours.foreach(flavour=> {
if (flavour.available) true
})
})
false


It shoudld break and return true as soon as I have true on available but its returning false all the time. Any suggestion?

Answer

I don't have your dataset to work with, but perhaps this might do it.

myChoice.category.exists(_.flavours.exists(_.available))