I have an array of books aka struct Book in the Top100Model below. I also have an array of books aka struct Data as well in the ISBNModel below. Both arrays have the same number of books and have a common property isbn13 which is a string holding a number. Id like to either merge the arrays so that it contains all the properties from Book in the Top100Model but add another two properties from the ISBNModel and copies the ‘subjects’ and ‘synopsis’ properties from the ISBNModel to this Top100Model based on the matching isbn13 values or merge them into a new dictionary. The code below to merge the arrays is returning empty
@State var mergedBooks = [T100FictMergeWithISBNDbyISBN13]()
@State var bookFromTop100Fiction = [Book]()
@State var bookFromISBNDBbyISBN13 = [Data]()
print(mergedBooks)
Top100Model below
struct Top100Model: Decodable {
let results: Result
private enum CodingKeys: String, CodingKey {
case results
}
}
struct Result: Decodable {
let listsb: [ListB]
private enum CodingKeys: String, CodingKey {
case listsb = "lists"
}
}
struct ListB: Codable {
let books: [Book]
}
struct Book: Codable, Identifiable {
let id = UUID()
let author: String
let description: String
let isbn13: String
let rank: Int
let title: String
}
ISBNModel Below
struct ISBNModel: Codable {
let total: Int
let requested: Int
let data: [Data]
}
struct Data: Codable {
let synopsis: String
let subjects: [String]
let isbn13: String
Merged model below
struct T100FictMergeWithISBNDBbyISBN13 {
let t100: Book
var isbndb: Data?
init(t100: Book) {
self.t100 = t100
}
var id: String {
return t100.isbn13
}
mutating func merge(status: Data) {
guard self.id == status.isbn13 else { return }
self.isbndb = status
}
}
1