Mohd Zakir
Mohd Zakir

Reputation: 128

Getting common data from two different types of array

struct Human{
    let name: String
    let id: String
    let travelled: String
}

struct Animal {
    let name: String
    let id: String
    let travelled: String
}

// Consider no human and animal object can have same name
let human1 = Human(name: "vikas", id: "12", travelled: "123")
let human2 = Human(name: "jacky", id: "15", travelled: "343")
let human3 = Human(name: "lucy", id: "32", travelled: "132")
let animal1 = Animal(name: "jacky", id: "56", travelled: "8979")
let animal2 = Animal(name: "lucy", id: "78", travelled: "678")
let animal3 = Animal(name: "jimmy", id: "98", travelled: "690")
let humans = [human1, human2, human3]
let animals = [animal1, animal2, animal3]

var list = [[String: String]]()
// can we eleminate this for loop with filter or something else
for human in humans {
    if let animal = animals.first(where: {$0.name == human.name}){
        let data = ["humanId": human.id, "animalId": animal.id]
        list.append(data)
    }
}
print(list)

Output:

[["humanId": "15", "animalId": "56"], ["humanId": "32", "animalId": "78"]]

Is there a way where we can apply multiple filter at a time to get the desired output Unable to find or create one

Upvotes: 3

Views: 85

Answers (3)

Joakim Danielson
Joakim Danielson

Reputation: 51973

This solutions still uses a loop but it will find multiple matches if such exists

animals.forEach {animal in
    let filtered = humans.filter { $0.name == animal.name }
        .map { human -> [String: String] in ["humanId": human.id, "animalId": animal.id] }

    list.append(contentsOf: filtered)
}

Upvotes: 1

Minh Huynh
Minh Huynh

Reputation: 1

Here it is

for index in 0..<humans.count {
    list[index]["humanId"] = humans[index]
    list[index]["animalId"] = animals[index]
}

Upvotes: -1

PGDev
PGDev

Reputation: 24341

You can try using compactMap(_:) instead.

let list = humans.compactMap { (human) -> [String:String]? in
    if let animal = animals.first(where: { $0.name == human.name }) {
        return ["humanId": human.id, "animalId": animal.id]
    }
    return nil
}

Upvotes: 3

Related Questions