0

Consider the following classes

class Category {
       var tag: String?
       var itemList: [Item]?
 }

 class Item {
       var id: Int?
       var type: String?
       var itemDetails: ItemDetails?  
 }

 class ItemDetails {
       var description: String?
       var name: String?
       var price: Float?
 }

Given an array of Category objects.

var categoryList: [Category]

I want to create a new object array by extracting only the name in ItemDetails(inorder to apply a filter) and an id inorder to reconstruct back array of Category objects.

Hence, I have to reconstruct the array of Category objects from new object array.

How to do both extraction and reconstruction using the map feature?

Below are the examples of other data sources:

Datasource 1 :

var categoryList: [Category], where name need to be extracted

Datasource 2 :

var searchList = [SearchItem], where title to be extracted.

    Class SearchItem {
         var id: Int?
         var details: SearchItemDetails?
         var type: String? 
    }

    Class  SearchItemDetails {
              
        var description: String?
        var title: String? 
    }

DataSource 3

var products: [Products], where title to be extracted.

    Class Products {
        var id: Int?
        var details: ProductDetails?
        var type: String?

    }

    class ProductDetails {
        var description: String?
        var title: String?

    } 
8
  • Why don't you just filter current categories? What is the need of reconstruction? Commented Oct 9, 2019 at 20:12
  • Actually, I have some pages with different data source. So I'am trying to create a utility class with one method to do filtering for all pages. Hence, I need to extract common datasource and reconstruct original to render results in corresponding pages. Commented Oct 10, 2019 at 5:18
  • So you need different filters. Can you show me an example of different datasources? Commented Oct 10, 2019 at 5:21
  • But the requirement is to use only one filter function. Hence, I need to construct a new datasource with item to be filtered(strings only) for every page. To reconstruct original, new datasource should contain filtering item and an id. categoryList in the question is an example of one such data source. Commented Oct 10, 2019 at 5:48
  • No I mean different examples so I can investigate more for a good mapping function Commented Oct 10, 2019 at 5:50

1 Answer 1

1

To get an array of just the names, you do the map like you mentioned:

let categories: [Category] = ... // this already exists
let itemNames = categories.map { $0.itemList?.map({ $0.itemDetails?.name }) }

But this will preserve optionals. If you don't want optionals, then use compactMap instead.

Reconstructing, however, doesn't really make any sense. Are you using the ID to hit a database or network service? That's the only possible way you'd be able to reconstruct the original array of Categorys. Why not just hold onto the original array?

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, I have some pages with different data source. So I'am trying to create a utility class with one method to do filtering for all pages. Hence, I need to extract common datasource and reconstruct original to render results in corresponding pages.

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.