0

//this is my model

struct RestaurantItem : Codable {
        
    var v : Int?
    var id : String = ""
    let addOnList : [AddOnList]? 
    var createdAt : String = ""
    var orderType : Int?
    var productId : ProductId?
    var quantity : Int?
    var restaurantId : String = ""
    var specialInstructions : String = ""
    var updatedAt : String = ""
    var userId : String = ""
    
    enum CodingKeys: String, CodingKey {
        case v = "__v"
        case id = "_id"
        case addOnList = "addOnList"
        case createdAt = "createdAt"
        case orderType = "orderType"
        case productId = "productId"
        case quantity = "quantity"
        case restaurantId = "restaurantId"
        case specialInstructions = "specialInstructions"
        case updatedAt = "updatedAt"
        case userId = "userId"
    }
    
    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        v = try values.decodeIfPresent(Int.self, forKey: .v)
        id = try values.decodeIfPresent(String.self, forKey: .id) ?? ""
        if (try? values.decodeIfPresent(String.self, forKey: .addOnList)) == nil {
            self.addOnList = try values.decodeIfPresent([AddOnList].self, forKey: .addOnList)
        } else {
            self.addOnList = nil
        }
        createdAt = try values.decodeIfPresent(String.self, forKey: .createdAt) ?? ""
        orderType = try values.decodeIfPresent(Int.self, forKey: .orderType)
        quantity = try values.decodeIfPresent(Int.self, forKey: .quantity)
        restaurantId = try values.decodeIfPresent(String.self, forKey: .restaurantId) ?? ""
        specialInstructions = try values.decodeIfPresent(String.self, forKey: .specialInstructions) ?? ""
        updatedAt = try values.decodeIfPresent(String.self, forKey: .updatedAt) ?? ""
        userId = try values.decodeIfPresent(String.self, forKey: .userId) ?? ""
        
        if (try? values.decodeIfPresent(String.self, forKey: .productId)) == nil {
            self.productId = try values.decodeIfPresent(ProductId.self, forKey: .productId)
        } else {
            self.productId = nil
        }
    }
    }

Decalare in viewcontroller

 var restaurantItems : [RestaurantItem]?

// this array contain dublicate value based on "id", How can i get a new array without replcate data

Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
Davender Verma
  • 503
  • 2
  • 12
  • Why do you have duplicate? The server responds duplicates? Or do you append too many times, and you obtains duplicate? You could create a dictionary base on the id, and then retrieve the value. But is the order important to you? – Larme Jul 17 '20 at 12:41
  • @Larme Server provide duplicate data. I know its a server side fault but now no one there to change this data replication . I have to manage it on my own side. yes Order is important – Davender Verma Jul 17 '20 at 12:44
  • https://stackoverflow.com/questions/34709066/remove-duplicate-objects-in-an-array ? – Larme Jul 17 '20 at 12:49
  • On what basis do you want to filter out the duplicate ids? – PGDev Jul 17 '20 at 12:51
  • @PGDev [0] = { productId = some { restaurantId = some { id = "5ed05bd5aa47246104f8c0fc" PName = “food” } } } [1] = { productId = some { restaurantId = some { id = "5ed05bd5aa47246104f8c0fc" PName = “drink” } } } Because id is same so i want consider it only 1 not 2. because Restaurant id is same for both so they are product of same Restaurant – Davender Verma Jul 21 '20 at 08:28

2 Answers2

0

You can use Dictionary.init(grouping:by:) to filter out the duplicate values, i.e.

if let items = restaurantItems {
    let arr = Dictionary(grouping: items){ $0.id }.values.first.flatMap{ $0 }
    print(arr)
}
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • Author in comments: " yes Order is important ", I know, that "detail" should be in the question. – Larme Jul 17 '20 at 12:50
  • if let items = self.restaurantItems { self.arrRestaurant = Dictionary(grouping: items){ $0.productId?.restaurantId?.id }.values.first.flatMap{ $0 } print(self.arrRestaurant) } – Davender Verma Jul 21 '20 at 06:26
  • restaurantId = some { id = "5ed05bd5aa47246104f8c0fc" – Davender Verma Jul 21 '20 at 06:28
  • this is is repeated . 3 times . But ineed a single result – Davender Verma Jul 21 '20 at 06:29
  • @PGDev if let items = self.restaurantItems { self.arrRestaurant = Dictionary(grouping: items){ $0.productId?.restaurantId?.id }.values.first.flatMap{ $0 } print(self.arrRestaurant) }. its contain all the values not the unique value – Davender Verma Jul 21 '20 at 07:47
0

If you struct conforms to the protocol Hashable and use use the id as the hash value. You can use this nice extension to give you an unique array.

    extension Array where Element : Hashable {
        var unique: [Element] {
            return Array(Set(self))
        }
    }

Jacob Van Brunt
  • 188
  • 2
  • 5