0

1) I want to show data from multiple arrays (the data is identically made, just split into different arrays). How do I do this?

2) I want to show only specific selections from across the arrays.

I have created a form of ID in these arrays called "groupid: Int" which appears across the arrays. In this case, I want to extract all the entries with "groupid: 1" from multiple arrays and show them in a list.

// The view that I want to show the final list in:

import SwiftUI

 struct French1View : View {

    var body: some View {
        NavigationView {
            List {
                ForEach(XXXXX????) { grape in
                    GrapeCell(grape: grape)
                }
                .navigationBarTitle("French wine grapes")
            }
    }
    }
}

// The arrays:

let BGrapes = [
    Grape(id: 1,
          groupid: 1,
          name: "Barbera",
          type: "White"),
    Grape(id: 2,
          groupid: 2,
          name: "Bosco",
          type: "Red")
 ]


let CGrapes = [
    Grape(id: 1,
          groupid: 1,
          name: "Barbera",
          type: "White")
]

As you can see in the code, I am stuck on what I should enter where I have (for this example) written "XXXXX????"

I have tried writing "(BGrapes, CGrapes) && grape.groupid(1)" but without success.

2 Answers 2

2

Welcome to SO. :)

You could concatenate the Grape arrays with + (Swift 5+) and filter by groupID:

var body: some View {
    NavigationView {
        List {
            ForEach((bGrapes + cGrapes).filter { $0.groupID == 1 }, id: \.self) { grape in
                GrapeCell(grape: grape)
            }
            .navigationBarTitle("French wine grapes")
        }
    }
}

Produces:

result

If you want results to be UNIQUE:

var body: some View {
    NavigationView {
        List {
            ForEach(uniqueGrapesFirstGroup(), id: \.self) { grape in
                GrapeCell(grape: grape)
            }
            .navigationBarTitle("French wine grapes")
        }
    }
}

func uniqueGrapesFirstGroup() -> [Grape] {
    let allGrapes = (bGrapes + cGrapes)
    let allGrapesFirstGroup = allGrapes.filter { $0.groupID == 1 }
    let allUniqueGrapesFirstGroup = Set(allGrapesFirstGroup)
    return Array(allUniqueGrapesFirstGroup)
}

Produces:

resultsUnique

Full code:

struct Grape: Hashable {
    let id: Int
    let groupID: Int
    let name: String
    let type: String
}

struct GrapeCell: View {
    let grape: Grape
    var body: some View {
        Group {
            Text("Name: \(grape.name)")
        }
    }
}

struct FrenchView: View {
    let bGrapes = [
        Grape(id: 1,
              groupID: 1,
              name: "Barbera",
              type: "White"),
        Grape(id: 2,
              groupID: 2,
              name: "Bosco",
              type: "Red")
    ]
    let cGrapes = [
        Grape(id: 1,
              groupID: 1,
              name: "Barbera",
              type: "White")
    ]

    var body: some View {
        NavigationView {
            List {
                ForEach(uniqueGrapesFirstGroup(), id: \.self) { grape in
                    GrapeCell(grape: grape)
                }
                .navigationBarTitle("French wine grapes")
            }
        }
    }

    func uniqueGrapesFirstGroup() -> [Grape] {
        let allGrapes = (bGrapes + cGrapes)
        let allGrapesFirstGroup = allGrapes.filter { $0.groupID == 1 }
        let allUniqueGrapesFirstGroup = Set(allGrapesFirstGroup)
        return Array(allUniqueGrapesFirstGroup)
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

this answer solved it all. thank you very, very much!
My pleasure! Please accept the answer: stackoverflow.com/help/someone-answers
0

You may combine arrays and filter them by groupid:

[BGrapes, CGrapes].flatMap { array in
    array.lazy.filter { $0.groupid == 1 }
}

1 Comment

you can .lazy the inner expression, to prevent the intermediate array allocation

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.