如何在 SwiftUI 中计算数组的总计、小计和平均值

Posted

技术标签:

【中文标题】如何在 SwiftUI 中计算数组的总计、小计和平均值【英文标题】:How to Calculate Totals, Subtotals, and Average from Array in SwiftUI 【发布时间】:2020-05-23 19:28:37 【问题描述】:

我是 SwiftUI 的新手,我已经被困了好几天了。

我有一个可识别的无序数组。 我在父视图中有一个针对该数组的过滤器,过滤器的值被传递到子视图。

我需要根据数组中一个元素的值对元素进行分组,然后根据每个数组元素中设置的另一个值计算总计、小计和平均值。

计算需要根据父视图中设置为 true 或 false 的过滤器进行更新。

我通过在 ForEach 视图中进行计算在 javascript (React) 中轻松解决了这个问题,但不知道如何在 SwitUI 中解决这个问题。

非常感谢任何帮助!

struct Student: Identifiable 
    let id: String
    let name: String
    let gradeLevel: Int
    let studyHours: Int


class GetStudents: ObservableObject 

    @Published var items = [Student]()

    init() 
        self.items = [
            Student(id: "aa1", name: "Bobby", gradeLevel: 9, studyHours: 2),
            Student(id: "aa2", name: "Sally", gradeLevel: 12, studyHours: 4),
            Student(id: "aa3", name: "Susy", gradeLevel: 11, studyHours: 3),
            Student(id: "aa4", name: "Billy", gradeLevel: 12, studyHours: 5),
            Student(id: "aa5", name: "Jimmy", gradeLevel: 11, studyHours: 1),
            Student(id: "aa6", name: "Johnny", gradeLevel: 10, studyHours: 3),
            Student(id: "aa7", name: "Mikey", gradeLevel: 12, studyHours: 2),
        ]
    


struct StudyTotals: View 

    @ObservedObject var students = GetStudents()

    var onlyUpperClass: Bool

    var studentsFiltered: [Student] 
        switch self.onlyUpperClass 
        case true: return students.items.filter $0.gradeLevel == 11 || $0.gradeLevel == 12 
        case false: return students.items
        
    

    var body: some View 

        var myArray = getCalcTotals(students: studentsFiltered)

        var rows: Array<String> = []

        return VStack 

            Text("Push Calculated Rows Here??? ")

        .padding()
    


struct ContentView: View 
    @State private var onlyUpperClass: Bool = false
    var body: some View 
        return VStack 
            StudyTotals(onlyUpperClass: self.onlyUpperClass)
        
    


struct ContentView_Previews: PreviewProvider 
    static var previews: some View 
        ContentView()
    



func getCalcTotals(students: Array<Student>) -> Array<String> 
    let studentArray = students.reduce( /* here */ )

    var grade: String
    var gradeCount: Int
    var gradeHours: Double? = 0
    var gradeAvg: Double? = 0

    let myArray = ["Count", "Total", "Average"]
    return myArray

我需要的是这个:

【问题讨论】:

【参考方案1】:

您可以像在 JavaScript 中一样计算 ForEach 中的值

struct StudyTotals: View 

    @ObservedObject var students = GetStudents()

    var onlyUpperClass: Bool

    var studentsFiltered: [Student] 
        switch self.onlyUpperClass 
        case true: return students.items.filter $0.gradeLevel == 11 || $0.gradeLevel == 12 
        case false: return students.items
        
    

    var body: some View 

        var countTotal = self.studentsFiltered.count
        var hoursTotal = self.studentsFiltered.reduce(0)  $0 + $1.studyHours 

        return List 
            Group 
                HStack 
                    Text("Count")
                    Text("Hours")
                    Text("Avg")
                
                ForEach(9...12, id: \.self)  gradeLevel -> AnyView in
                    let count = self.studentsFiltered.filter $0.gradeLevel ==  gradeLevel .count
                    let hours = self.studentsFiltered.filter $0.gradeLevel ==  gradeLevel .reduce(0)  $0 + $1.studyHours 
                    let avg = Float(hours) / Float(count)
                    countTotal += count
                    hoursTotal += hours
                    return AnyView(HStack 
                        Text("Grade\(gradeLevel)")
                        Text("\(count)")
                        Text("\(hours)")
                        Text("\(avg)")
                    )
                
                HStack 
                    Text("TOTALS")
                    Text("\(countTotal)")
                    Text("\(hoursTotal)")
                    Text("\(Float(hoursTotal) / Float(countTotal))")
                
            
        .padding()
    

【讨论】:

简直太棒了!你的回答对我来说是一个改变游戏规则的人!谢谢!

以上是关于如何在 SwiftUI 中计算数组的总计、小计和平均值的主要内容,如果未能解决你的问题,请参考以下文章

如何在foreach中计算数组

在Python中计算数组元素[重复]

SQL SEVER分类汇总后如何让小计和总计放到下面

oracle中如何添加小计和总计

SQL如何查询总计和小计

在 C# 中计算数组频率分布的最快方法是啥?