How to convert one array of objects to another in Swift?

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

In Swift, there are several ways to convert one array of objects to another array of objects. One way is to use a for-in loop and map the elements of the original array to the new array. The map function allows you to apply a transformation to each element of an array and returns a new array with the transformed elements.

struct Person {
    let firstName: String
    let lastName: String
}

let people = [Person(firstName: "John", lastName: "Doe"),
              Person(firstName: "Jane", lastName: "Smith")]

let fullNames = people.map { "\($0.firstName) \($0.lastName)" }
print(fullNames) // ["John Doe", "Jane Smith"]

Read more on The power of map in Swift and Map, FlatMap, Filter, Reduce – High order functions in Swift 5+

Another way to convert an array of objects is to use the compactMap function. This function works similarly to the map function, but it also filters out nil values.

struct Person {
    let firstName: String
    let lastName: String
    var fullName: String?
}

let people = [Person(firstName: "John", lastName: "Doe"),
              Person(firstName: "Jane", lastName: "Smith")]

let firstNames = people.compactMap { $0.firstName }
print(firstNames) // [John, Jane]

Another approach is to use the filter function, this function allows you to filter the elements of an array based on a certain condition and returns a new array with only the elements that match that condition

struct Person {
    let firstName: String
    let lastName: String
    let age: Int
}

let people = [Person(firstName: "John", lastName: "Doe", age: 30),
              Person(firstName: "Jane", lastName: "Smith", age: 25),
              Person(firstName: "Robert", lastName: "Johnson", age: 35)]

let adults = people.filter { $0.age >= 18 }
print(adults) // [Person(firstName: "John", lastName: "Doe", age: 30), Person(firstName: "Jane", lastName: "Smith", age: 25), Person(firstName: "Robert", lastName: "Johnson", age: 35)]

Lastly, you can also use the reduce function to convert one array of objects to another array of objects. The reduce function takes an initial value and a closure that takes two parameters. It applies the closure to the initial value and the first element of the array, then applies the closure to the result and the next element of the array, and so on.

struct Person {
    let firstName: String
    let lastName: String
}

let people = [Person(firstName: "John", lastName: "Doe"),
              Person(firstName: "Jane", lastName: "Smith")]

let initial: [String] = []
let fullNames = people.reduce(initial) { (result, person) in result + ["\(person.firstName) \(person.lastName)"] }
print(fullNames) // ["John Doe", "Jane Smith"]

All these methods will help you to convert an array of objects to another array of objects in Swift



✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn



Get Ready to Shine: Mastering the iOS Interview




Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book.

If you know someone who would benefit from reading this article, please share it with them.