1

I have two struct and two arrays corresponding to it and I am trying to compare the two array values and print it in one filtered array i did try and use filter but its giving me an error I want to compare an id from both the struct and get one single array

Struct ONE

struct One {
    let ID: String
    let name: String
    let lastName: String
}

Array One

var oneData = [One]()
oneData = [One(ID: "1", name: "hello1", lastName: "last2"), One(ID: "1", name: "hello2", lastName: "last2"), One(ID: "2", name: "hello3", lastName: "last3"), One(ID: "3", name: "hello4", lastName: "last4")]

Struct TWO

struct Two {
    let ID: String
    let name2: String
    let lastName2: String
}

Array Two

var twoData = [Two]()
twoData = [Two(ID: "1", name2: "hello1", lastName2: "last1"), Two(ID: "2", name2: "hello2", lastName2: "last2"), Two(ID: "3", name2: "hello3", lastName2: "last3"), Two(ID: "4", name2: "hello4", lastName2: "last4"), Two(ID: "5", name2: "hello5", lastName2: "last5")]

My filtered Array

var mainArray = [Two]()

Code to that I used to filter which is giving me an error

mainArray = oneData.filter{ $0.ID == twoData.contains(where: $0.ID)}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Audrey Rozario
  • 59
  • 1
  • 10
  • 1
    What objects do you want in the final result? – rmaddy May 24 '18 at 05:36
  • I want to match oneData ID with twoData ID and print one single array with the matched values @rmaddy – Audrey Rozario May 24 '18 at 05:55
  • Actually you have two different types of `Array`. So if you filter with any kind of conditions, you will get back one of the types of `Array`. That means either you will get `[One]` or `[Two]`. For this kind of situation you should always consider using only one type of `struct`. – nayem May 24 '18 at 10:51

4 Answers4

3

If you're looking to filter oneData to only those elements that have a matching ID field in twoData, you want:

let mainArray = oneData.filter { i in twoData.contains { i.ID == $0.ID } }
David Berry
  • 40,941
  • 12
  • 84
  • 95
2

To get objects of array oneData whose ID matched with ID of any object from array twoData. You can do following:

    // Array One
    var oneData = [One]()
    oneData = [One(ID: "1", name: "hello1", lastName: "last2"),
               One(ID: "1", name: "hello2", lastName: "last2"),
               One(ID: "2", name: "hello3", lastName: "last3"),
               One(ID: "3", name: "hello4", lastName: "last4")]

    // Array Two
    var twoData = [Two]()
    twoData = [Two(ID: "1", name2: "hello1", lastName2: "last1"),
               Two(ID: "2", name2: "hello2", lastName2: "last2"),
               Two(ID: "3", name2: "hello3", lastName2: "last3"),
               Two(ID: "4", name2: "hello4", lastName2: "last4"),
               Two(ID: "5", name2: "hello5", lastName2: "last5")]

    let mainArray = oneData.filter { i in twoData.contains{ i.ID == $0.ID } }
    print(mainArray)

enter image description here

Ankit Jayaswal
  • 5,549
  • 3
  • 16
  • 36
1

your mainArray is of type [Two] and you are trying to put data of type [One] in mainArray. According to swift this is wrong you cannot do this.

you can do it in another way let me post it in 10 mins

You have 2 options now either make common protocol like and confirm the structs with it and make the mainArray of that protocol type as follows:

protocol Identifiable {
    var id: String {get set}
    var name: String {get set}
    var lastName: String {get set}
}

struct One: Identifiable{
    var id: String
    var name: String
    var lastName: String
}

struct Two: Identifiable{
    var id: String
    var name: String
    var lastName: String
}

now make the main Array of type Identifiable as follows

 var mainArray = [Identifiable]()

and filter as follows

 mainArray = oneData.filter{ i in twoData.contains { i.id == $0.id }}

Another option is do not make the protocol Identifiable and let the structs be as they were before. And just make the mainArray of type [One] just dont forget to change the change in filter line i.e From this:

 mainArray = oneData.filter{ $0.ID == twoData.contains(where: $0.ID)}

To this:

 mainArray = oneData.filter{ i in twoData.contains { i.id == $0.id }}
sanjaykmwt
  • 586
  • 3
  • 19
  • i am not getting my desired output my output look like this MAIN ARRAY=[MatchStruct.One(id: "1", name: "name2", lastname: "last name2"), MatchStruct.One(id: "2", name: "name2", lastname: "last name2"), MatchStruct.One(id: "1", name: "name2", lastname: "last name2"), MatchStruct.One(id: "2", name: "name2", lastname: "last name2")] – Audrey Rozario May 24 '18 at 06:23
  • I want to match oneData ID with twoData ID and if they match it should come in my filtered array – Audrey Rozario May 24 '18 at 06:24
  • you need data of twoData if their id matches with oneData's ids, correct? – sanjaykmwt May 24 '18 at 06:30
  • if you need data of twoData then do it like this **mainArray = twoData.filter{ i in oneData.contains { i.id == $0.id }}** – sanjaykmwt May 24 '18 at 06:33
  • I want to match id of oneData and id of twoData and if they match it should print it in a new array – Audrey Rozario May 24 '18 at 07:04
  • you are not understanding my concern – sanjaykmwt May 24 '18 at 08:54
  • 1
    suppose if oneData has `{id: 1, name: name1, lastname: last1}` and twoData has `{id: 1, name: name2, lastname: last3}` so both of them have id 1 now in the mainArray what you want `{id: 1, name: name1, lastname: last1}` or `{id: 1, name: name2, lastname: last3}` – sanjaykmwt May 24 '18 at 08:57
  • can you help me with my new question https://stackoverflow.com/questions/50540100/i-am-trying-to-get-particular-logged-in-user-data-in-the-profile-in-swift-4 – Audrey Rozario May 26 '18 at 06:48
0

Swift standard library has an Equatable protocol that we can adopt by adding the static == operator function to our type in an extension. You should create only one structure like below

struct Employee {
  let id: String
  let name: String
  var lastName: String
}

extension Employee: Equatable {
  static func == (lhs: Employee, rhs: Employee) -> Bool {
    return lhs.name == rhs.name &&
    lhs.id == rhs.id &&
    lhs.lastName == rhs.lastName
  }
}
Amrit Trivedi
  • 1,240
  • 1
  • 9
  • 24
  • You should have extended your answer a little bit more to match the OP's requirement. Like [finding common elements in two arrays](https://stackoverflow.com/questions/32439289/how-to-get-list-of-common-elements-of-2-array-in-swift). – nayem May 24 '18 at 11:17