Bo Wei
Bo Wei

Reputation: 23

Get the sum of the values of the same label from the struct

I need to add the unappeared to the Array from this information. If there is a duplicate, the money will be summed up and then saved into one. How can I achieve this? The data may be a lot.

import UIKit

struct TestData {
    let Label : String
    var Money : Double
}

var SaveDate = [TestData]()
var TestD = [TestData]()
SaveDate.append(TestData(Label: "test1", Money: 55))
SaveDate.append(TestData(Label: "test1", Money: 35))
SaveDate.append(TestData(Label: "test2" , Money: 15))
SaveDate.append(TestData(Label: "test1" , Money: 10))
SaveDate.append(TestData(Label: "test3" , Money: 30))
print([{Label "test1", Money 55}, {Label "test1", Money 35}, {Label "test2", Money 15},{Label "test1", Money 10}, {Label "test3", Money 30}])


//Result presentation
//Label1 = test1 , Money = 100
//Label2 = test2 , Money = 15
//Label3 = test3 , Money = 30

Upvotes: 2

Views: 103

Answers (3)

Rakesha Shastri
Rakesha Shastri

Reputation: 11242

It's as simple as this.

let dict = SaveDate.reduce(into: [String: Double]()) { (result, data) in
    result[data.Label, default: 0] += data.Money
}

Or more concisely,

let dict = SaveDate.reduce(into: [String: Double]()) { $0[$1.Label, default: 0] += $1.Money }

dict

// ["test2": 15.0, "test3": 30.0, "test1": 100.0]


Note: You should use lowerCamelCase for variable names as per the Swift API guidelines.

Upvotes: 2

impression7vx
impression7vx

Reputation: 1863

You can use a dictionary to accomplish this.

import UIKit

struct TestData {
    let Label : String
    var Money : Double
}

var SaveDate = [TestData]()
SaveDate.append(TestData(Label: "test1", Money: 55))
SaveDate.append(TestData(Label: "test1", Money: 35))
SaveDate.append(TestData(Label: "test2" , Money: 15))
SaveDate.append(TestData(Label: "test1" , Money: 10))
SaveDate.append(TestData(Label: "test3" , Money: 30))

var dictionaryOfValues:[String:Double] = [:]

func addUp() {
    for currentItem in SaveDate {
        dictionaryOfValues[currentItem.Label] = (dictionaryOfValues[currentItem.Label] == nil ? currentItem.Money : dictionaryOfValues[currentItem.Label] + currentItem.Money)
    }
}

addUp()
print(dictionaryOfValues) //["test1": 100.0, "test2": 15.0, "test3": 30.0]

I ran this in a Swift Playground.

A dictionary can keep track of your when you ran into a label or not. The reason I do dictionaryOfValues[currentItem.Label] = (dictionaryOfValues[currentItem.Label] == nil ? currentItem.Money : dictionaryOfValues[currentItem.Label] + currentItem.Money) is because it needs to either 1) add it to the dictionary or 2) append the current value to a value already in the dictionary.

Whenever you are ready, you can call addUp() and it will do the work for you.

Upvotes: 0

KeroppiMomo
KeroppiMomo

Reputation: 594

This might help:

import UIKit

struct TestData {
    let label : String
    var money : Double
}

var saveData = [TestData]()
saveData.append(TestData(label: "test1", money: 55))
saveData.append(TestData(label: "test1", money: 35))
saveData.append(TestData(label: "test2" , money: 15))
saveData.append(TestData(label: "test1" , money: 10))
saveData.append(TestData(label: "test3" , money: 30))

let testData = saveData.reduce([TestData](), { currentData, newElement in
    var newDataArray = currentData // Swift doesn't allow modifying parameter
    if newDataArray.count == 0 {
        newDataArray.append(newElement)
    } else {
        if let sameLabelIndex = newDataArray.firstIndex(where: { $0.label == newElement.label }) {
            newDataArray[sameLabelIndex].money += newElement.money
        } else {
            newDataArray.append(newElement)
        }
    }
    return newDataArray
})

print(testData)

Apple's documentation on reduce: https://developer.apple.com/documentation/swift/array/2298686-reduce

Upvotes: 0

Related Questions