Tuesday, July 2, 2019

Map,Filter In Swift

Higher order functions in Swift


Map the map function returns an array containing the results of applying a mapping or transform function to each item.Map is just like for loop. For Example -

"data": [
    {
      "id": 118,
      "name": "milestone 3",
     },
    {
      "id": 117,
      "name": "testAdmim",
   }

Take out all the values of key "name"-

  let filterOrgId = arrayOfName.map({ $0.name})
  result - ["milestone","testAdmim"]

Filter function loops over every item in a collection/Array, and returns a collection containing only items that satisfy an include condition

Example- 
"data": [
    {
      "id": 118,
      "permission": "yes",
     },
    {
      "id": 117,
       "permission": "no",
   }
]

  let filertedData = org_P.filter{$0.id == "your id"}
       filertedData.forEach {

            print("model data objects::",$0. permission  as? String) // if your condition is true than you will get permission value with particular id
                                     

                 }

  

No comments:

Post a Comment