Reputation: 165
I am getting a response of NSDictionary list of products as
{
"products": {
"title": "Chair",
"regular_price": "2.22",
"dimensions": {
"width": "",
"height": "",
"length": ""
},
"attributes": [{
"options": ["11\/30\/2016"],
"name": "Arrival Date"
}, {
"options": ["Black"],
"name": "Color"
}],
"categories": ["28"]
}
}.....
Using NSPredicate I could filter the products containing value "Chair" using
let namepredicate = NSPredicate(format: "title == Chair")
self.filteredProducts = (self.product).filteredArrayUsingPredicate(namepredicate)
But How can I filter "Color", "Black" which inside the attributes and "Black" is inside another array(Swift)?
Upvotes: 0
Views: 1055
Reputation: 63399
First of all, rename self.product
to self.products
. It's an array of multiple products, name it accordingly.
You can replace the existing NSPredicate
mess with just:
self.filteredProducts = self.products.filter{ $0["title"] == "Chair" }
And you can filter by Color like so:
self.filteredProducts = self.products.filter{ product in
return product["attributes"]?.contains{ attribute in
return attribute["name"] == "Color" &&
attribute["options"]?.contains("Black") ?? false
} ?? false
}
Upvotes: 1