My question is more for understanding the documentation. He says:
fun <K, V> Map<out K, V>.forEach(
action: (Entry<K, V>) -> Unit)
However, I do not understand how to implement it. How do I get the key and value inside the loop?
I want to sum for each item in listItems
the price value. Map associates a string with an item
data class Item(val name: String, val description: String, val price: String, val index: Int)
Imagine what it listItems
contains:
listItems ["shirt"] → name: shirt, description: lorem ipsum, price: 10, index: 0
listItems ["shoes"] → name: shoes, description: lorem ipsum, price: 30, index: 0
Thus, the code will look something like this:
var total: Int = 0
listItems.forEach {
total += parseInt(value.price)
}
However, I do not understand how to access this documentation- value
relatedV
source
share