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

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

Array of objects to Dictionary in Swift

问题

Here's the translated code portion:

  1. 我有一个类似的数组
  2. var data: [UserModal] = [
  3. UserModal(unit: "Unit 1", data: [
  4. ScoreModal(name: "Ney", score: 10 ),
  5. ScoreModal(name: "Mes", score: 20 ),
  6. ScoreModal(name: "Ro", score: 30 ),
  7. ScoreModal(name: "Su", score: 40 ),
  8. ]),
  9. UserModal(unit: "Unit 2", data: [
  10. ScoreModal(name: "Ney", score: 10 ),
  11. ScoreModal(name: "Mes", score: 20 ),
  12. ScoreModal(name: "Ro", score: 30 ),
  13. ScoreModal(name: "Su", score: 40 ),
  14. ]),
  15. UserModal(unit: "Unit 3", data: [
  16. ScoreModal(name: "Ney", score: 10 ),
  17. ScoreModal(name: "Mes", score: 20 ),
  18. ScoreModal(name: "Ro", score: 30 ),
  19. ScoreModal(name: "Su", score: 40 ),
  20. ]),
  21. ]
  22. 我想计算成员的总分,如下:
  23. var result: [ScoreModal] = [
  24. ScoreModal(name: "Ney", score: 30 ),
  25. ScoreModal(name: "Mes", score: 60 ),
  26. ScoreModal(name: "Ro", score: 90 ),
  27. ScoreModal(name: "Su", score: 120 ),
  28. ]
  29. 我尝试使用 map 进行操作,但没有成功,请帮助我

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

英文:

I have an array of like

  1. var data: [UserModal] = [
  2. UserModal(unit: "Unit 1", data: [
  3. ScoreModal(name: "Ney", score: 10 ),
  4. ScoreModal(name: "Mes", score: 20 ),
  5. ScoreModal(name: "Ro", score: 30 ),
  6. ScoreModal(name: "Su", score: 40 ),
  7. ]),
  8. UserModal(unit: "Unit 2", data: [
  9. ScoreModal(name: "Ney", score: 10 ),
  10. ScoreModal(name: "Mes", score: 20 ),
  11. ScoreModal(name: "Ro", score: 30 ),
  12. ScoreModal(name: "Su", score: 40 ),
  13. ]),
  14. UserModal(unit: "Unit 3", data: [
  15. ScoreModal(name: "Ney", score: 10 ),
  16. ScoreModal(name: "Mes", score: 20 ),
  17. ScoreModal(name: "Ro", score: 30 ),
  18. ScoreModal(name: "Su", score: 40 ),
  19. ]),
  20. ]

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

  1. var result: [ScoreModal] = [
  2. ScoreModal(name: "Ney", score: 30 ),
  3. ScoreModal(name: "Mes", score: 60 ),
  4. ScoreModal(name: "Ro", score: 90 ),
  5. ScoreModal(name: "Su", score: 120 ),
  6. ]

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

答案1

得分: 1

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

  1. var nameArray = data.map { model in
  2. var array = [String]()
  3. guard let arrayModel = model.data else {
  4. return array
  5. }
  6. for data in arrayModel {
  7. array.append(data.name ?? "")
  8. }
  9. return array
  10. }
  11. let flattenedArray = nameArray.reduce([], +)
  12. print(flattenedArray)
  13. let uniqueArray = Array(Set(flattenedArray))
  14. print(uniqueArray)
  15. var scoreModal: [ScoreModal] = []
  16. uniqueArray.forEach{ key in
  17. var totalScore = 0
  18. data.forEach{ user in
  19. user.data?.filter{ $0.name == key}.forEach{ totalScore += $0.score ?? 0 }
  20. }
  21. scoreModal.append(ScoreModal(name: key, score: totalScore))
  22. }
  23. print("ScoreModal: \(scoreModal)")
英文:

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

  1. var nameArray = data.map { model in
  2. var array = [String]()
  3. guard let arrayModel = model.data else {
  4. return array
  5. }
  6. for data in arrayModel {
  7. array.append(data.name ?? "")
  8. }
  9. return array
  10. }
  11. let flattenedArray = nameArray.reduce([], +)
  12. print(flattenedArray)
  13. let uniqueArray = Array(Set(flattenedArray))
  14. print(uniqueArray)
  15. var scoreModal: [ScoreModal] = []
  16. uniqueArray.forEach{ key in
  17. var totalScore = 0
  18. data.forEach{ user in
  19. user.data?.filter{ $0.name == key}.forEach{ totalScore += $0.score ?? 0 }
  20. }
  21. scoreModal.append(ScoreModal(name: key, score: totalScore))
  22. }
  23. print("ScoreModal: \(scoreModal)")

答案2

得分: 1

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

  1. var result: [ScoreModal] = []
  2. ["Ney","Mes","Ro","Su"].forEach{ key in
  3. var totalScore = 0
  4. data.forEach{ user in
  5. user.data.filter{ $0.name == key}.forEach{ totalScore += $0.score }
  6. }
  7. result.append(ScoreModal(name: key, score: totalScore))
  8. }
  9. print("---> result: \(result)")

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

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

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

  1. var result: [ScoreModal] = []
  2. ["Ney","Mes","Ro","Su"].forEach{ key in
  3. var totalScore = 0
  4. data.forEach{ user in
  5. user.data.filter{ $0.name == key}.forEach{ totalScore += $0.score }
  6. }
  7. result.append(ScoreModal(name: key, score: totalScore))
  8. }
  9. print("---> result: \(result)")

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

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

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:

确定