0

I have a situation like i have array of dictionaries, in which each key has array of specific object. like this.

    // example setup
    struct FruitsData {
        var name: String?
        var id: String?
    }

tableViewSource = [String : [FruitsData]]

so i have to apply filter on this inner array. but i am unable to update the value in final array. I have written this code.

tableViewSource = tableViewSource.filter { ( dictData: (key: String, value: [FruitsData])) -> Bool in
    var arrFruitsData = dictData.value
    arrFruitsData = arrFruitsData.filter{ ( $0.id != nil) }

    if arrFruitsData.count == 0 {
        self.tableViewHeaders = self.tableViewHeaders.filter { $0 != dictData.key }
    }
    return true

    }

like i have remove those values in array whose id has been removed.

for eg if i have these values in array.

var array = ["A": [FruitsData(name: "apple", id: "5"), FruitsData(name: "apricot",id: "")], "M": [FruitsData(name: "mango", id: "9"),    FruitsData(name: "grapes", id: "")]]
0

1 Answer 1

2

First of all tableViewSource is not the Array of Dictionary, it is Dictionary with each key having Array as value. Also from you example id is not nil but empty if you want remove object of FruitsData whose id is nil or empty you can make it like this.

var tableViewSource = [String : [FruitsData]]()
tableViewSource.forEach {
    let res = $1.filter { $0.id != nil && $0.id != "" }
    tableViewSource[$0] = res
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot.. it solved my problem. but is this not an array of dictionaries?
@user1960279 Welcome mate :) Yes it not array of dictionaries it is dictionary having all values as array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.