将对象数组转换为Swift中的字典

huangapple go评论64阅读模式
英文:

Array of objects to Dictionary in Swift

问题

Here's the translated code portion:

我有一个类似的数组

var data: [UserModal] = [
    UserModal(unit: "Unit 1", data: [
        ScoreModal(name: "Ney", score: 10 ),
        ScoreModal(name: "Mes", score: 20 ),
        ScoreModal(name: "Ro", score: 30 ),
        ScoreModal(name: "Su", score: 40 ),
    ]),
    UserModal(unit: "Unit 2", data: [
        ScoreModal(name: "Ney", score: 10 ),
        ScoreModal(name: "Mes", score: 20 ),
        ScoreModal(name: "Ro", score: 30 ),
        ScoreModal(name: "Su", score: 40 ),
    ]),
    UserModal(unit: "Unit 3", data: [
        ScoreModal(name: "Ney", score: 10 ),
        ScoreModal(name: "Mes", score: 20 ),
        ScoreModal(name: "Ro", score: 30 ),
        ScoreModal(name: "Su", score: 40 ),
    ]),
]

我想计算成员的总分,如下:

var result: [ScoreModal] = [
    ScoreModal(name: "Ney", score: 30 ),
    ScoreModal(name: "Mes", score: 60 ),
    ScoreModal(name: "Ro", score: 90 ),
    ScoreModal(name: "Su", score: 120 ),
]

我尝试使用 map 进行操作,但没有成功,请帮助我

If you have any further questions or need assistance with the code, feel free to ask.

英文:

I have an array of like

 var data: [UserModal] = [
        UserModal(unit: "Unit 1", data: [
            ScoreModal(name: "Ney", score: 10 ),
            ScoreModal(name: "Mes", score: 20 ),
            ScoreModal(name: "Ro", score: 30 ),
            ScoreModal(name: "Su", score: 40 ),
        ]),
        UserModal(unit: "Unit 2", data: [
            ScoreModal(name: "Ney", score: 10 ),
            ScoreModal(name: "Mes", score: 20 ),
            ScoreModal(name: "Ro", score: 30 ),
            ScoreModal(name: "Su", score: 40 ),
        ]),
        UserModal(unit: "Unit 3", data: [
            ScoreModal(name: "Ney", score: 10 ),
            ScoreModal(name: "Mes", score: 20 ),
            ScoreModal(name: "Ro", score: 30 ),
            ScoreModal(name: "Su", score: 40 ),
        ]),
    ]

I want to calculate the total score of the members like:

  var result: [ScoreModal] = [
        ScoreModal(name: "Ney", score: 30 ),
        ScoreModal(name: "Mes", score: 60 ),
        ScoreModal(name: "Ro", score: 90 ),
        ScoreModal(name: "Su", score: 120 ),
    ]

I tried to manipulate the map but it didn't work
please help me

答案1

得分: 1

接受 @Sumida's 的答案,我刚刚更新了动态键。

var nameArray = data.map { model in
    var array = [String]()
    guard let arrayModel = model.data else {
        return array
    }
    for data in arrayModel {
        array.append(data.name ?? "")
    }
    return array
}
let flattenedArray = nameArray.reduce([], +)
print(flattenedArray)

let uniqueArray = Array(Set(flattenedArray))
print(uniqueArray)

var scoreModal: [ScoreModal] = []

uniqueArray.forEach{ key in
    var totalScore = 0
    data.forEach{ user in
        user.data?.filter{ $0.name == key}.forEach{ totalScore += $0.score ?? 0 }
    }
    scoreModal.append(ScoreModal(name: key, score: totalScore))
}

print("ScoreModal: \(scoreModal)")
英文:

Accept @Sumida's answer i just made update for dynamic keys.

var nameArray = data.map { model in
    var array = [String]()
    guard let arrayModel = model.data else {
        return array
    }
    for data in arrayModel {
        array.append(data.name ?? "")
    }
    return array
}
let flattenedArray = nameArray.reduce([], +)
print(flattenedArray)

let uniqueArray = Array(Set(flattenedArray))
print(uniqueArray)

var scoreModal: [ScoreModal] = []

uniqueArray.forEach{ key in
    var totalScore = 0
    data.forEach{ user in
        user.data?.filter{ $0.name == key}.forEach{ totalScore += $0.score ?? 0 }
    }
    scoreModal.append(ScoreModal(name: key, score: totalScore))
}

print("ScoreModal: \(scoreModal)")

答案2

得分: 1

使用简单的forEach循环和filter,将计算成员的总分,就像您展示的results一样:

var result: [ScoreModal] = []

["Ney","Mes","Ro","Su"].forEach{ key in
    var totalScore = 0
    data.forEach{ user in
        user.data.filter{ $0.name == key}.forEach{ totalScore += $0.score }
    }
    result.append(ScoreModal(name: key, score: totalScore))
}

print("---> result: \(result)")

请注意,如果您不想使用关键字数组,可以替换为:

Set(data.map{$0.data.map{$0.name}}.flatMap{$0}).forEach{ key in
   ....
}
英文:

Using simple forEach loops and a filter, will calculate the total score of the members, like the results you show:

var result: [ScoreModal] = []

["Ney","Mes","Ro","Su"].forEach{ key in
    var totalScore = 0
    data.forEach{ user in
        user.data.filter{ $0.name == key}.forEach{ totalScore += $0.score }
    }
    result.append(ScoreModal(name: key, score: totalScore))
}

print("---> result: \(result)")

Note, if you don't want to use the array of keys, replace it with:

 Set(data.map{$0.data.map{$0.name}}.flatMap{$0}).forEach{ key in
   ....
 }

huangapple
  • 本文由 发表于 2023年6月15日 11:50:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定