Merge arrays with condition

后端 未结 3 2051
忘掉有多难
忘掉有多难 2021-01-20 02:30

I would like to merge two arrays with specific condition and update objects that they are containing.

First my struct that is in arrays:

struct Item          


        
3条回答
  •  执念已碎
    2021-01-20 03:17

    A solution for your specific problem above would be:

    struct Item {
        var id: Int
        var name: String
    }
    
    let first = Item(id: 1, name: "Oleg")
    let second = Item(id: 2, name: "Olexander")
    
    let firstInSecond = Item(id: 1, name: "Bogdan")
    let secondInSecond = Item(id: 2, name: "Max")
    
    let ret = zip([first, second], [firstInSecond, secondInSecond]).map({
        return $0.id == $1.id ? $1 : $0
    })
    

    => But it requires that there are as many items in the first as in the second array - and that they have both the same ids in the same order...

提交回复
热议问题