Swift Array intersection by property

user3179636

I am trying compare two arrays. One array is an array of Person objects, each of which has an email property that is a String email address. The other array is an EmailAddress object which has a descriptive word like "work" or "personal" and the actual String email address.

Basically both objects have a String property for email address. I want to compare these arrays of objects to see if one of the objects from each array has the same email address. Right now I am using nested for loops as shown below but that is taking too long.

for person in self.allPeople! {
    for e in EmailAddresses! {
        if e.value == person.email {
             return true               
        }
    }
}

I thought about using set intersection but that looked like it would only work for comparing the same objects and not object's properties. Thanks.

andyvn22

You can still use Set functionality by first creating a set of all the emails. map helps turn one collection into another, in this case changing your collection of allPeople into a collection of those people's emails. This will be faster because now EmailAddresses is iterated once, instead of once per person.

let personEmails = Set(self.allPeople!.map { $0.email })
let matchingEmails = EmailAddresses!.map { $0.value }
return !personEmails.isDisjoint(with: matchingEmails)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript / JQuery Array of objects intersection by property

From Java

Set operations (union, intersection) on Swift array?

From Dev

Swift optional Array property is immutable?

From Dev

Best practice for array as property in Swift

From Dev

Swift - Get Object in Array By Property

From Dev

Swift optional Array property is immutable?

From Dev

Best practice for array as property in Swift

From Dev

Match nodes where there is any intersection between its array property and other array

From Dev

swift access property of an object in Array, returns nil

From Java

Removing array object according to its property in Swift

From Dev

Swift computed property to return copy of underlying array

From Dev

Swift dynamic property setting based on array keys

From Dev

Swift 2.0 Sorting Array of Objects by Property

From Dev

Swift - Check if array contains element with property

From Dev

Swift filter array of objects by objects INT! property

From Dev

Order swift array by custom property with condition and secondarily custom property desc

From Dev

Intersection of two lists by object property

From Dev

ActiveRecord query array intersection?

From Dev

Testing for no array intersection in PHP

From Dev

find the intersection of a cell array

From Dev

Intersection of two dimensional array

From Dev

Array of objects intersection

From Dev

Ruby - array intersection (with duplicates)

From Dev

ActiveRecord query array intersection?

From Dev

find the intersection of a cell array

From Dev

Multiple array intersection in javascript

From Dev

How to assign an array of int to a corresponding property of an array of objects in Swift?

From Java

Simplest code for array intersection in javascript

From Java

P5 array intersection

Related Related

  1. 1

    Javascript / JQuery Array of objects intersection by property

  2. 2

    Set operations (union, intersection) on Swift array?

  3. 3

    Swift optional Array property is immutable?

  4. 4

    Best practice for array as property in Swift

  5. 5

    Swift - Get Object in Array By Property

  6. 6

    Swift optional Array property is immutable?

  7. 7

    Best practice for array as property in Swift

  8. 8

    Match nodes where there is any intersection between its array property and other array

  9. 9

    swift access property of an object in Array, returns nil

  10. 10

    Removing array object according to its property in Swift

  11. 11

    Swift computed property to return copy of underlying array

  12. 12

    Swift dynamic property setting based on array keys

  13. 13

    Swift 2.0 Sorting Array of Objects by Property

  14. 14

    Swift - Check if array contains element with property

  15. 15

    Swift filter array of objects by objects INT! property

  16. 16

    Order swift array by custom property with condition and secondarily custom property desc

  17. 17

    Intersection of two lists by object property

  18. 18

    ActiveRecord query array intersection?

  19. 19

    Testing for no array intersection in PHP

  20. 20

    find the intersection of a cell array

  21. 21

    Intersection of two dimensional array

  22. 22

    Array of objects intersection

  23. 23

    Ruby - array intersection (with duplicates)

  24. 24

    ActiveRecord query array intersection?

  25. 25

    find the intersection of a cell array

  26. 26

    Multiple array intersection in javascript

  27. 27

    How to assign an array of int to a corresponding property of an array of objects in Swift?

  28. 28

    Simplest code for array intersection in javascript

  29. 29

    P5 array intersection

HotTag

Archive