struct Address {
    var name: String
    var imageURL: String
    var address: String
}

let VIPArray = [["name": "John B"], ["name": "Sara K"]]

let AddressArray = [Address(name: "John B",  imageURL: "johnb", address: "178 Main St."),
                    Address(name: "Dave H",  imageURL: "daveh", address: "1011 Victoria St.."),
                    Address(name: "Sara K",  imageURL: "sarak", address: "279 Maple Av."),
                    Address(name: "Niles K", imageURL: "nilesk", address: "45 King St."),
                    Address(name: "Ingrid G",  imageURL: "ingridg", address: "33 Union St.")]


var filtered = [Address]()

for element in VIPArray {
    for address in AddressArray {
        if element["name"] == address.name {
            filtered.append(address)
        }
    }
}

for record in filtered {
    print(record)
}

OUTPUT:

Address(name: "John B", imageURL: "johnb", address: "178 Main St.")

Address(name: "Sara K", imageURL: "sarak", address: "279 Maple Av.")

Or:

let filtered: [Address] = AddressArray.filter { (address) -> Bool in
    for vip in VIPArray {
        if vip["name"] == address.name {
            return true
        }
    }
    return false
}
Answer from Dmitry on Stack Overflow
🌐
Swift Forums
forums.swift.org › using swift
Filter array with another array - Using Swift - Swift Forums
June 29, 2022 - I would like to create a new array of strings that dont contain strings from another array. I have it working in the long form. But would like to learn a more Swift way of doing it The return filteredList would be ["music", "unicorns rainbow walkers"] let removelist : [String] = ["nerf", "magic beans"] let originalList : [String] = ["nerf", "music", "unicorns rainbow walkers", "magic beans"] // tried this but not exactly working //let filtered = originalList.filter { $0.contains(where: { rem...
Top answer
1 of 3
4
struct Address {
    var name: String
    var imageURL: String
    var address: String
}

let VIPArray = [["name": "John B"], ["name": "Sara K"]]

let AddressArray = [Address(name: "John B",  imageURL: "johnb", address: "178 Main St."),
                    Address(name: "Dave H",  imageURL: "daveh", address: "1011 Victoria St.."),
                    Address(name: "Sara K",  imageURL: "sarak", address: "279 Maple Av."),
                    Address(name: "Niles K", imageURL: "nilesk", address: "45 King St."),
                    Address(name: "Ingrid G",  imageURL: "ingridg", address: "33 Union St.")]


var filtered = [Address]()

for element in VIPArray {
    for address in AddressArray {
        if element["name"] == address.name {
            filtered.append(address)
        }
    }
}

for record in filtered {
    print(record)
}

OUTPUT:

Address(name: "John B", imageURL: "johnb", address: "178 Main St.")

Address(name: "Sara K", imageURL: "sarak", address: "279 Maple Av.")

Or:

let filtered: [Address] = AddressArray.filter { (address) -> Bool in
    for vip in VIPArray {
        if vip["name"] == address.name {
            return true
        }
    }
    return false
}
2 of 3
4
let VIPArray = [["name": "John B"], ["name": "Sara K"]]

struct Address {
    let name: String
    let imageURL: String
    let address: String
}

let addressArray = [Address(name: "John B",  imageURL: "johnb", address: "178 Main St."),
                    Address(name: "Dave H",  imageURL: "daveh", address: "1011 Victoria St.."),
                    Address(name: "Sara K",  imageURL: "sarak", address: "279 Maple Av."),
                    Address(name: "Niles K", imageURL: "nilesk", address: "45 King St."),
                    Address(name: "Ingrid G",  imageURL: "ingridg", address: "33 Union St.")]

let myVips = addressArray.filter() {VIPArray.contains(["name":$0.name])}
🌐
Reddit
reddit.com › r/swift › filtering array of objects by strings in another array.
r/swift on Reddit: Filtering array of objects by strings in another array.
October 30, 2015 -

I have an array of Match objects [Match] with a string property of matchID. I also have an array of strings.

I would like to remove the Match object where matchID equals any string in the array.

I currently have:

                var matches = [Match]
                var matchIDsToDelete = [String]

                for match in matches {
                    for id in matchIDsToDelete {
                        if match.matchID == id {
                            // remove that item from array
                        }
                    }
                }

My concern is removing the object from the array as I'm enumerating. I believe there will be consequences. Any suggestions? I would also love a functional solution as I'm currently reading more about functional programming.

Thanks!

🌐
Swift Forums
forums.swift.org › using swift
How to effectively filter array by another array? ( In functional paradigm ) - Using Swift - Swift Forums
December 27, 2018 - Consider following task. Let A be a list of Comparable items. Let B be a list of Comparable items. Entries in these lists can be compared. Output is a list of entries in A that are not in the list B. Two conditions. …
🌐
Apple Developer
developer.apple.com › forums › thread › 86114
How to filter one array based on a… | Apple Developer Forums
CustomCell if(searchActive){ cell.toolLabel.text = filtered[indexPath.row] } else { cell.toolLabel.text = data[indexPath.row] } return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let vcName = identities[indexPath.row] let viewController = storyboard?.instantiateViewController(withIdentifier: vcName) self.navigationController?.pushViewController(viewController!, animated: true) } } Any help would be greatly appreciated. ... This seems to be the preferred way. //... //### change the 2 arrays into 1 typealias Item = (data: String, identity: Stri
Find elsewhere
🌐
Donny Wals
donnywals.com › how-to-filter-an-array-in-swift
How to filter an Array in Swift? – Donny Wals
April 23, 2024 - If you return false, the element is ignored and won't be included in the new Array. When you apply a filter on an Array, the original Array is not modified. Instead, filter(isIncluded:) creates a new Array with only the elements you want.
🌐
Stack Overflow
stackoverflow.com › questions › 60399770 › how-can-i-filter-array-according-to-another-array-in-swift
ios - How can I filter array according to another array in swift? - Stack Overflow
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { if mainSearchBar.text == nil || mainSearchBar.text == "" { isSearching = false view.endEditing(true) tableView.reloadData() }else { isSearching = true filteredArray = orderId.filter({$0.range(of: mainSearchBar.text!, options: .caseInsensitive) != nil}) tableView.reloadData() } } ... I see 3 arrays in the code, filteredArray, orderId and newDatas but I have no idea which 2 you mean and what you mean by conflicts.
🌐
Programiz
programiz.com › swift-programming › library › array › filter
Swift Array filter() (With Examples)
The filter() method returns all the elements from the array that satisfy the provided condition. The filter() method returns all the elements from the array that satisfy the provided condition. Example var numbers = [2, 3, 6, 9] // return all the elements greater than 5 var result = ...
Top answer
1 of 2
3

If you don't mind the O(n^2), then test directly in one step.

let arrayOne = [["keyphraseId":"tcpid1234", "name":"shakti"], ["keyphraseId":"tcpid456", "name":"shakti"], ["keyphraseId":"tcpid897", "name":"srichandan "], ["keyphraseId":"tcpid779", "name":"prakash"]]
let arrayTwo = [["idstring":"tcpid1234", "name":"shakti"],["idstring":"tcpid456", "name":"shakti"]]

arrayOne.filter { one in
    !arrayTwo.contains { two in
        one["keyphraseId"] == two["idstring"]
    }
}

If you want better performance O(n), drop stringids into a set

let arrayOne = [["keyphraseId":"tcpid1234", "name":"shakti"], ["keyphraseId":"tcpid456", "name":"shakti"], ["keyphraseId":"tcpid897", "name":"srichandan "], ["keyphraseId":"tcpid779", "name":"prakash"]]
let arrayTwo  = [["idstring":"tcpid1234", "name":"shakti"],["idstring":"tcpid456", "name":"shakti"]]

let idstrings = Set(arrayTwo.flatMap { $0["idstring"] })

arrayOne.filter {
    guard let keyphraseId = $0["keyphraseId"] else { return false }
    return !idstrings.contains(keyphraseId)
}
2 of 2
2

You have 2 arrays as follows:

    let arrayOne = [
        ["keyphraseId":"tcpid1234", "name":"shakti"],
        ["keyphraseId":"tcpid456", "name":"shakti"],
        ["keyphraseId":"tcpid897", "name":"srichandan "],
        ["keyphraseId":"tcpid779", "name":"prakash"]
    ]

    let arrayTwo  = [
        ["idstring":"tcpid1234", "name":"shakti"],
        ["idstring":"tcpid456", "name":"shakti"]
    ]

To filter the array you need to follow 2 steps:

  1. List out idStrings from arrayTwo

  2. filter arrayOne with fetched idString from step 1

To get elements from arrayOne

whose keyphraseId lies in arrayTwo as idstring :

    let arrayTwoIds = arrayTwo.map { $0["idstring"] }
    let filteredResults = arrayOne.filter { arrayTwoIds.contains($0["keyphraseId"]) }
    print(filteredResults)

whose keyphraseId not lies in arrayTwo as idstring :

Just put ! in filter condition:

    let filteredResults = arrayOne.filter { !arrayTwoIds.contains($0["keyphraseId"]) }

Hope this will help.

🌐
TutorialKart
tutorialkart.com › swift-tutorial › swift-array-filter
How to Filter a Swift Array based on a Condition?
May 2, 2021 - var arr = [1, 99, 6, 74, 5] let result = arr.filter { $0 > 10 } print("Original Array : \(arr)") print("Filtered Array : \(result)") ... Concluding this Swift Tutorial, we learned how to filter a given array based on a specific condition.
🌐
Hacking with Swift
hackingwithswift.com › example-code › language › how-to-remove-items-from-an-array-using-filter
How to remove items from an array using filter() - free Swift example code and tips
May 28, 2019 - The filter() method goes over all the items in an array (or indeed any kind of collection), and returns a new array containing items that pass a test you specify. ... NEW You don’t need more Swift tutorials.
🌐
Apple Developer
developer.apple.com › forums › thread › 693772
filter an object array based on another
How can you judge if an Option exist in the selected Array? Compare userCode? Compare optType? Compare optValue? Or some other way? ... extension Selected { func matches(_ option: Option) -> Bool { //↓Modify the following expression to fit for your purpose return optType == option.optType && optValue == option.optValue } } ... Button("Options Not Selected") { let optionsNotSelected = options.filter { option in !selected.contains {$0.matches(option)} } print(optionsNotSelected) //Use `optionsNotSelected`... }