https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

antoniomarin

04/02/2021, 10:24 AM
One algorithm question. If I have a following nested object lists, and I want to remove "3" from every `object2`:
Copy code
object1: TestType  = [
	object2=[
		(...)
		random = ...,
		stringList = ["1", "2", "3"]
	],
    object2(),
    object2(),
    (....)
]
Something like this should work, but I'm wondering is there a more elegant solution? I'm sure there is 😄 I guess I could do a
object2Mapper
which could filter "3" as well.
Copy code
fun TestType.getWithout3(): TestType {
	val returnObject = object1
	returnObject.forEach { object1 ->
		object1.object2.forEach { object2 ->
			object2.stringList.toMutableList.removeAll { item == "3"}
		}
	}
	return returnObject
}
Solved by using
mappers
and doing this logic while transforming objects. Not sure is there a better or more performant solution 🤷
9 Views