-1

I have a List of JValues in scala with the following format: [{ id: "foo" values: {...}}, {}]

I want to go through the list, and, if the id is equal to a certain value, replace the values. In Java, I have this code

newList = List()

for (item in list) {
  if (item.id == id) { 
    newList.add(newValues)

  }
  else {
    newList.add(item)
  }
}

} }

How would I do this in Scala?

Suma
  • 33,181
  • 16
  • 123
  • 191
Daniel
  • 458
  • 7
  • 21
  • Assuming your list is Scala immutable list, you can use the `map` on the list to create a new list. Inside your `map` you pass a function that does the check you want to do. – Soumya Simanta Feb 16 '15 at 02:11

1 Answers1

0
val ary: JArray = //some value
ary.children.map {
    case JObject if myid == JObject.getField("id") => new JObject(myid, newvalue)
    case other => other
}
LynxZh
  • 825
  • 5
  • 9