如何在Swift中按条件组合数组中的对象?

时间:2022-09-25 12:54:17

I have an array of objects:

我有一个对象数组:

struct Person {
    let name: String
    let dateOfBirth: Date
    let money: Double
}

It contains people and how much money they have received or spent. The array contains 54 such objects. I need to find all objects with the same name and date fields and sum their money. That is, in the end, I have to get less than 54 records in my array.

它包含了人们以及他们收到或花了多少钱。该数组包含54个这样的对象。我需要找到具有相同名称和日期字段的所有对象并总结他们的钱。也就是说,最后,我必须在我的阵列中获得少于54条记录。

Example: In the array, there are 3 records with the object John. For example, first, he received $15.0, then spent $7.0, and then received $4.0. For my task, I need to have 1 record in the array instead of 3 (with money filed as $12.0)

示例:在数组中,有3条记录与对象John。例如,首先,他收到15美元,然后花了7.0美元,然后收到4.0美元。对于我的任务,我需要在数组中有1条记录而不是3条(以12.0美元的价格提交)

Does anyone have any idea?

有人有什么主意吗?

2 个解决方案

#1


1  

You can loop through the array and add them to a dictionary, where the name concatenated with the date of birth will be the key and the sum of the money will be the value.

您可以循环遍历数组并将它们添加到字典中,其中与出生日期连接的名称将是关键字,并且钱的总和将是值。

Assuming your array containing Person objects is called people:

假设包含Person对象的数组称为people:

var moneyDictionary = [String:Double]()
for person in people {
    let personKey = "\(person.name)\(person.dateOfBirth)"
    if let personRecord = moneyDictionary[personKey] {
        moneyDictionary[personKey] = personRecord + person.money
    } else {
        moneyDictionary[personKey] = person.money
    }
}

#2


0  

Maybe something like this would work:

也许这样的事情会起作用:

struct Person {
    let name: String
    let dateOfBirth: Date
    let money: Double
}

let currentDate = Date()
let person = Person(name: "James", dateOfBirth: currentDate, money: 15)
let person1 = Person(name: "James", dateOfBirth: currentDate, money: -7)
let person2 = Person(name: "James", dateOfBirth: currentDate, money: 4)

// array of person objects
let personArray = [person, person1, person2]

// selected person
let selectedPerson = person

// filter with selected person only
var matchingPersons = personArray.filter {
    $0.name == selectedPerson.name && $0.dateOfBirth == selectedPerson.dateOfBirth
}

// extract values
var values = matchingPersons.map { $0.money }

// total
var total = values.reduce(0, +)

#1


1  

You can loop through the array and add them to a dictionary, where the name concatenated with the date of birth will be the key and the sum of the money will be the value.

您可以循环遍历数组并将它们添加到字典中,其中与出生日期连接的名称将是关键字,并且钱的总和将是值。

Assuming your array containing Person objects is called people:

假设包含Person对象的数组称为people:

var moneyDictionary = [String:Double]()
for person in people {
    let personKey = "\(person.name)\(person.dateOfBirth)"
    if let personRecord = moneyDictionary[personKey] {
        moneyDictionary[personKey] = personRecord + person.money
    } else {
        moneyDictionary[personKey] = person.money
    }
}

#2


0  

Maybe something like this would work:

也许这样的事情会起作用:

struct Person {
    let name: String
    let dateOfBirth: Date
    let money: Double
}

let currentDate = Date()
let person = Person(name: "James", dateOfBirth: currentDate, money: 15)
let person1 = Person(name: "James", dateOfBirth: currentDate, money: -7)
let person2 = Person(name: "James", dateOfBirth: currentDate, money: 4)

// array of person objects
let personArray = [person, person1, person2]

// selected person
let selectedPerson = person

// filter with selected person only
var matchingPersons = personArray.filter {
    $0.name == selectedPerson.name && $0.dateOfBirth == selectedPerson.dateOfBirth
}

// extract values
var values = matchingPersons.map { $0.money }

// total
var total = values.reduce(0, +)