ios - How do I replace a changing array with another value? -
i have date array contains current day plus 10 days, , importing array database. have animal name array. based on imported want place animal name in filtered array in filtered array. example:
date array: `[9, 10, 11, 12, 13, 14, 15, 16, 17, 18]` imported date array db: [9, 12, 14, 18] imported animal name array db: [dog, cat, tiger, sheep]
this want filtered animal name like
filtered animal name array: [dog, "", "", cat, "", tiger, "", "", "", sheep]
i know code provided wrong, feel approaching incorretly. how should this?
for(var j = 0; j < self.arrayofweek.count; j++){ for(var t = 0; t < self.datesfromdb.count; t++){ if(self.date[t] == self.datearray[j]){ self.namefiltered[t] = self.tutorname[j] print("filtered name \(self.namefiltered)") } } }
data:
let dates = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18] let imported = [9, 12, 14, 18] let animals = ["dog", "cat", "tiger", "sheep"]
reworked:
let filtered = dates.map { zip(imported, animals).map { $0.0 }.indexof($0).map { animals[$0] } ?? "" }
output:
print(array) // ["dog", "", "", "cat", "", "tiger", "", "", "", "sheep"]
based on franmowinckel's answer, 100% safe.
Comments
Post a Comment