0

Here is my model

class BusinessProfile: NSObject {
    var title: String?
    var website: String?
    var associatedOrganization: String?
    var companyName: String?
    
    var productList: [BusinessProfileProduct]?
}

class BusinessProfileProduct: NSObject{    
    var productName: Double?
    var modelNumber: String?
    var hsCode: String?
}

Here are my array variables in view controller.

var businessProfileArray = [BusinessProfile]()
var tempBusinessProfileArray = [BusinessProfile]()

I already have filtered businessProfileArray on the basis of companyName like below:

tempBusinessProfileArray = businessProfileArray.filter({ (BusinessProfile) -> Bool in
            return (BusinessProfile.companyName!.lowercased().contains(searchText.lowercased()))
        })

But I am unable to filter businessProfileArray on the basis of productName or hsCode from nested array of BusinessProfileProduct.

Note: businessProfileArray contains array of businessProfileProduct

Any help from anyone? Thanks in advance.

1 Answer 1

1

You can do something similar to this

func filterArr(searchText:String) -> [BusinessProfile] {
    var filteredArr = [BusinessProfile]()
    for profile in businessProfileArray {
        var isMatched = false
        
        if let matchedProduct = profile.productList.filter ({$0.companyName.lowercased().contains(searchText.lowercased())}).first {
            isMatched = true
            print(matchedProduct.companyName)
        }
        
        if isMatched {
            filteredArr.append(profile)
        }
        
    }
    
    return filteredArr
}

this will return all the profiles in which there is a match of searchText with product's companyName however it will not remove the extra products which does not match the searchText

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

2 Comments

@Sptibo happy to help :)
@Sptibo if it solved your problem please consider an upvote, thank you

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.