stackerleet
stackerleet

Reputation: 518

How do I replace a changing array with another value?

I have a date array which contains the current day plus 10 days, and I am importing an array from a database. I also have an animal name array. Based on what is imported I want to place the animal name in a filtered array in a filtered array. For example:

  date array: `[9, 10, 11, 12, 13, 14, 15, 16, 17, 18]`
  imported date array from db: [9, 12, 14, 18]
  imported animal name array from db: [dog, cat, tiger, sheep]

This is what I want the filtered animal name to look like

filtered animal name array: [dog, "", "", cat, "", tiger, "", "", "", sheep]

I know the code I provided is wrong, I feel I am approaching this incorretly. How should I do 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)")
                            }
                        }
                    }

Upvotes: 0

Views: 79

Answers (3)

Eendje
Eendje

Reputation: 8883

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, but 100% safe.

Upvotes: 4

FranMowinckel
FranMowinckel

Reputation: 4343

Edit, even shorter:

dates.map { datesDb.indexOf($0).map { animalsDb[$0] } ?? "" }

Just use a map function (this doesn't create any auxiliary array or dictionary):

var dates = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
var datesDb = [9, 12, 14, 18]
var animalsDb = ["dog", "cat", "tiger", "sheep"]

let result = dates.map { date -> String in
  if let index = datesDb.indexOf(date) where index < animalsDb.count {
    return animalsDb[index]
  } else {
    return ""
  }
}

Upvotes: 1

aahrens
aahrens

Reputation: 5590

I tried this in a playground. What it does it iterates over each Int in your date array. It then tries to find that same Int in the date array db. If it can find it then it tries to take the index and lookup in the animal array. I updated some variable names so you'll need to translate them.

let date = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
let foundDates = [9, 12, 14, 18]
let animals = ["dog", "cat", "tiger", "sheep"]

var filteredAnimals = [String]()
for currentDate in date {
    // Look up the date  in the foundDates array
    // If it's found, ensure that index is in bounds of the animal array
    if let index = foundDates.indexOf(currentDate) where (0..<animals.count).contains(index) {
        filteredAnimals.append(animals[index])
    } else {
        filteredAnimals.append("")
    }
}

Upvotes: 3

Related Questions