Swift:根据唯一值将2个或更多元素自定义对象合并到数组中 [英] Swift: Merging 2 or more elements custom objects in array based on unique value

查看:91
本文介绍了Swift:根据唯一值将2个或更多元素自定义对象合并到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的自定义对象类.

Below is my custom object class.

class UserGroups: NSObject {
    let groupName: String
    let users: [CheckIn]?

    init(json:JSON) {
        self.groupName = json[Constants.Models.UserGroups.groupName].stringValue
        self.users = UserGroups.getUserGroupsList(jsonArray: json[Constants.Models.UserGroups.users].arrayValue)
    }

    class func getUserGroupsList(jsonArray: [JSON]) -> [CheckIn]{
        return jsonArray.flatMap({ (jsonItem: JSON) -> CheckIn in
            return CheckIn(json: jsonItem)
        })
    }
}

我有一个以上自定义对象的数组.通过合并具有相同 groupName 的每个对象的用户,如何将2个或更多的自定义对象组合到单个对象中.

I've an array of above custom objects. How can I combine 2 or more custom objects into a single object by merging users of every object having same groupName.

下面是我的CheckIn模型:

Below is my CheckIn Model:

类签入:NSObject {

class CheckIn: NSObject {

let id: String
let firstName: String
let lastName: String
let latitude: String
let longitude: String
let hint: String

init(json: JSON) {
    self.id = json[Constants.Models.CheckIn.id].stringValue
    self.firstName = json[Constants.Models.CheckIn.firstName].stringValue
    self.lastName = json[Constants.Models.CheckIn.lastName].stringValue
    self.hint = json[Constants.Models.CheckIn.hint].stringValue
    self.latitude = json["location"][Constants.Models.CheckIn.latitude].stringValue
    self.longitude = json["location"][Constants.Models.CheckIn.longitude].stringValue
}

}

id 字段在CheckIn中不是唯一的.

id field is not unique in CheckIn.

推荐答案

下面是一个稍微简化的示例,显示了如何组合具有相同组名的组.

Here's a slightly simplified example that shows how to combine groups that have the same group name.

这是 UserGroup 类. users 现在是一个变量( var ),因为我们将元素添加到组中以将它们组合在一起.

Here is the UserGroup class. users is now a variable (var) because we will be adding elements to groups to combine them.

class UserGroups: NSObject {
    let groupName: String
    var users: [String]?

    init(groupName: String, users: [String]?) {
        self.groupName = groupName
        self.users = users
    }
}

这里有三个组,其中两个共享相同的组名, Blues .

Here are three groups, two of the share the same group name, Blues.

let group1 = UserGroups(groupName: "Blues", users: ["Tom", "Huck", "Jim"])
let group2 = UserGroups(groupName: "Reds", users: ["Jo", "Ben", "Tommy"])
let group3 = UserGroups(groupName: "Blues", users: ["Polly", "Watson", "Douglas"])

接下来,我们将所有组放在一个数组中.

Next, we'll put all the groups in an array.

let allGroups = [group1, group2, group3]

在这里,我们使用Swift的 reduce 函数来允许我们将数组简化为仅具有唯一组名的组.

Here, we use Swift's reduce function to allow us to reduce the array to only groups with unique group names.

let compacted = allGroups.reduce([UserGroups](), { partialResult, group in

    var dupe = partialResult.filter {$0.groupName == group.groupName }.first
    if let dupeGroup = dupe {
        dupeGroup.users?.append(contentsOf: group.users ?? [])
        return partialResult
    } else {
        var newPartialResult = partialResult
        newPartialResult.append(group)
        return newPartialResult
    }
})

现在该数组减少为唯一的组,我们借助Swift的

The array is now reduced to unique groups, we print out all the groups and their users with the help of Swift's map function.

print(compacted.map { $0.users })

// Prints [
Optional(["Tom", "Huck", "Jim", "Polly", "Watson", "Douglas"]), 
Optional(["Jo", "Ben", "Tommy"])
]

这篇关于Swift:根据唯一值将2个或更多元素自定义对象合并到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆