0

I make perform HTTP Request by this command

let task = URLSession.shared.dataTask(with: request) { (data, response, error)

and get back my data in JSONDecoder, then I do this

let groceryPro = try decoder.decode(GroceryProduct.self, from: data!)

where my GroceryProduct is:

    struct GroceryProduct: Codable  { 
   var data: [Data1] 
   } 
   struct Data1: Codable {
   var id: String? 
   var name: String? 
   var system_name: String? 
   var type: String? 
   var lang: String? 
   var is_global: Int?
}

I got a big array, this is one value from it, in total there are ≈ 700:

Data1(id: Optional("9"), name: Optional("войти"), system_name: Optional("login"), type: Optional("button"), lang: Optional("ru"), is_global: Optional(1))

I need to call the array that matches some requirements, for an example, if system_name : "Esc" then I need to get this array

2
  • 4
    At a lower level of knowledge, you create an empty array, you do a for loop on that big array, and each time there is an object that match your requirement, you append it to the array. That's the basic of algorithmic. At a higher level, you can use filter(). Commented Nov 17, 2021 at 7:21
  • @EmilioPelaez yes, it's helped, thanks! Commented Nov 18, 2021 at 8:14

1 Answer 1

-1

try this :

let arrEseSystem = groceryPro.data.filter{ $0.system_name == “Esc" }
Sign up to request clarification or add additional context in comments.

3 Comments

"Esc” *******
thanks, it works, exactly what I need.
glad that work for you, can you make my answer as accepted answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.