Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

How can pivoting an array in swift

With this code I create the array

struct Item {
    let expense  : String
    let year     : String
    let value   : Float
}
var resultArray = [Item]()
resultArray.append(Item(expense: "Restaurant", year: "2019", value: 1000))
resultArray.append(Item(expense: "Restaurant", year: "2020", value: 500))
resultArray.append(Item(expense: "Restaurant", year: "2021", value: 100))
resultArray.append(Item(expense: "Gym",        year: "2019", value: 100))
resultArray.append(Item(expense: "Gym",        year: "2020", value: 100))
resultArray.append(Item(expense: "Gas",        year: "2020", value: 200))
resultArray.append(Item(expense: "Gas",        year: "2021", value: 50))

If I were to represent the result in the form of an excel table, I would get this

enter image description here

I would like obtain this, but I don't know how:

enter image description here

Thank you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...