嵌套属性匹配条件的过滤器列表
Filter list where nested attributes match condition
我有以下情况类:
case class Reduction(value: Int, label: String)
case class Variant(id: String, name: String, reductions: List[Reduction])
case class Item(id: String, name: String, variants: List[Variant])
我从解析的 JSON 中得到 List[Item]
。现在我想过滤掉每个变体,它的缩减与特定的缩减标签不匹配。
用下面的例子想象一下:
List(
Item("1234", "Train ticket from A-B",
List(
Variant("1","short way", List(Reduction(50,"Half Fare"), Reduction(0,"Adult"))),
Variant("2","long way", List(Reduction(0,"Adult")))
)
),
Item("5678", "Train ticket from B-C",
List(
Variant("1","short way", List(Reduction(50,"Half Fare"))),
Variant("2","long way", List(Reduction(0,"Adult")))
)
)
)
我最终需要的形式是List[Item]
:
所有项目,其变体具有给定的减少量。例如。如果我需要过滤“半价”,我希望过滤后的列表[Item]包含
仅包含变体 1 的项目 1234 和仅包含变体 1 的项目 5678。过滤仅需在变体级别进行。
我知道我必须以某种方式使用 fold
或 reduce
,但我正在努力获取代码 运行。
我的尝试看起来有点像这样:
items.reduce(i => i.variants.reduce(v => checkLabel(v.reductions, requiredLabel) )
由于我是函数式编程的新手,特此向您请教如何正确解决此问题。任何解释都会有所帮助。
您可能需要使用这样的东西:
考虑示例中的以下情况类:
case class Reduction(value: Int, label: String)
case class Variant(id: String, name: String, reduction: Reduction)
case class Item(id: String, variants: List[Variant])
您可以在items: List[Item]
上按如下方式进行:
items.filter(_.variants.exists(variant => checkLabel(variant.reduction, requiredLabel)))
很难理解你的意思,但是这个过滤了所有包含一些变体的项目,checkLabel
returns 对他们来说是正确的
尝试根据您的特定用途更改它或编辑 post 以提供更清晰的详细信息,我会尽力提供帮助:)
如果您想保留没有匹配变体的商品,您可以这样做:
val requiredLabel = "Half Fare"
items.map(item =>
item.copy(variants = item.variants.filter(_.reduction.exists(_.label == requiredLabel)))
).filter(_.variants.nonEmpty)
代码 运行 在 Scastie。
我有以下情况类:
case class Reduction(value: Int, label: String)
case class Variant(id: String, name: String, reductions: List[Reduction])
case class Item(id: String, name: String, variants: List[Variant])
我从解析的 JSON 中得到 List[Item]
。现在我想过滤掉每个变体,它的缩减与特定的缩减标签不匹配。
用下面的例子想象一下:
List(
Item("1234", "Train ticket from A-B",
List(
Variant("1","short way", List(Reduction(50,"Half Fare"), Reduction(0,"Adult"))),
Variant("2","long way", List(Reduction(0,"Adult")))
)
),
Item("5678", "Train ticket from B-C",
List(
Variant("1","short way", List(Reduction(50,"Half Fare"))),
Variant("2","long way", List(Reduction(0,"Adult")))
)
)
)
我最终需要的形式是List[Item]
:
所有项目,其变体具有给定的减少量。例如。如果我需要过滤“半价”,我希望过滤后的列表[Item]包含
仅包含变体 1 的项目 1234 和仅包含变体 1 的项目 5678。过滤仅需在变体级别进行。
我知道我必须以某种方式使用 fold
或 reduce
,但我正在努力获取代码 运行。
我的尝试看起来有点像这样:
items.reduce(i => i.variants.reduce(v => checkLabel(v.reductions, requiredLabel) )
由于我是函数式编程的新手,特此向您请教如何正确解决此问题。任何解释都会有所帮助。
您可能需要使用这样的东西:
考虑示例中的以下情况类:
case class Reduction(value: Int, label: String)
case class Variant(id: String, name: String, reduction: Reduction)
case class Item(id: String, variants: List[Variant])
您可以在items: List[Item]
上按如下方式进行:
items.filter(_.variants.exists(variant => checkLabel(variant.reduction, requiredLabel)))
很难理解你的意思,但是这个过滤了所有包含一些变体的项目,checkLabel
returns 对他们来说是正确的
尝试根据您的特定用途更改它或编辑 post 以提供更清晰的详细信息,我会尽力提供帮助:)
如果您想保留没有匹配变体的商品,您可以这样做:
val requiredLabel = "Half Fare"
items.map(item =>
item.copy(variants = item.variants.filter(_.reduction.exists(_.label == requiredLabel)))
).filter(_.variants.nonEmpty)
代码 运行 在 Scastie。