Home > Software engineering >  Scala accessing attribute value in nested Seq collection [closed]
Scala accessing attribute value in nested Seq collection [closed]

Time:09-21

I have the nested Seq collection below and I'm trying to access the value of the 'height' attribute inside the 'Banner' atttribute. Consider that there may exist multiple 'Banners' in the List and multiple 'Campaigns' in the Seq collection. What is the best way to get the value of 'height' of each 'Banner' in the Banner's List? I've tried to do it like below but it only prints the value of the entire banner, 'Banner(1,https://business.eskimi.com/wp-content/uploads/2020/06/openGraph.jpeg,300,250)'. I want it to print the value of '250'. Thanks in advance!


        case class Campaign(id: Int, country: String, targeting: Targeting, banners: List[Banner], bid: Double)
        case class Targeting(targetedSiteIds: Seq[String])
        case class Banner(id: Int, src: String, width: Int, height: Int)


        val lists = Seq(
                  Campaign( // Campaign
                      1, // Id
                      "LT", // Country
                      Targeting( // Targeting
                          Seq("0006a522ce0f4bbbbaa6b3c38cafaa0f") // TargetedSiteIds
                          ),
                      List( // Banners
                            Banner( // Banner
                                1, // id
                                "https://business.eskimi.com/wp-content/uploads/2020/06/openGraph.jpeg", // URL source
                                300, // width
                                250 // height
                                )
                            ),
                        5d // price
                        )
                    )

        activeCampaigns.foreach(_.banners.foreach(el=>println(el)))

CodePudding user response:

This question is not well written, I have no idea what you're trying to achieve, but maybe you can make something out of this.

If I wanted to print those values I would do something like this

activeCampaigns.foreach(_.banners.foreach(el => println(el.height)))
  • Related