0

I need to form this:

{
“item” : “hardcoded_value”,
“item2” : “hardcoded_value”,
“item3” : “hardcoded_value”,
}

In the exec block I am trying:

// list with items [“item1”, “ item2”, “ item3”]
val theList = session("itemNames").as[List[String]]

val theMap = Map.empty[String,String]     // empty map

// add items from list in map
theList.foreach{ key =>
                      | theMap += key -> "hardcoded_value"
}

But getting error at += position.

Also tried:

theList.foreach(key =>  theMap += key -> "hardcoded_value" )

How to insert key and value into a map by iterating over a list? I am new to gatling and scala.

daman
  • 309
  • 2
  • 5
  • 13

1 Answers1

0

After looking at your question in more detail, I realize that you're not just asking about turning the Collection into a Map. Combining the answers from How can I use map and receive an index as well in Scala? and Scala best way of turning a Collection into a Map-by-key?, you can do the following:

List("first", "second", "third").zipWithIndex.map {
  case (item, index) => ("item" + index) -> item.toString
}
>res0: List[(String, String)] = List((item0,first), (item1,second), (item2,third))
Community
  • 1
  • 1
childofsoong
  • 1,918
  • 16
  • 23