Converting one struct to another using map/reduce functions

Viewed 1046

How can i map/reduce values of one struct array to another using swift's higher order function?. Currently iam iterating over the array and appending each values to new array. Is there any "swifty" method to map the elements to other?

/// Code sample
let priorityList = [Priority]()
let pushRowList = [PushRowList]()

for priority in priorityList {
  let id = priority.priorityID
  let state = priority.priorityState
  let item = PushRowList(optionId: id, optionTitle: state)
  pushRowList.append(item)
}

Iam expecting the "swifty" methods like map, reduce etc to perform the operation.

1 Answers

You can simply use map, since you just want to access certain properties of each element of an array and use them to create another type.

let pushRowLists = priorityList.map{PushRowList(optionId: $0.priorityID, optionTitle: $0.priorityState}
Related