Home > OS >  How to use a for each loop to iterate only over the even or odd indexes of an array in Swift
How to use a for each loop to iterate only over the even or odd indexes of an array in Swift

Time:07-25

I have one array that I want to iterate over and make a button object for each element of the array. However, I want to have two VStacks that split these buttons up, essentially two columns with many rows of the text objects, but I want it to go from left to right in sequential order. In one column would be the odd numbers (or even indexes i.e. 3rd number is index 2), in the second column would be the even numbers (or odd indexes). Here is my code as of right now, but it doesn't split them up it just makes duplicates.

 HStack{
                    VStack{
                        ForEach(game.ButtonAnswers, id: \.id){ answer in
                            ButtonRows(answer: answer)
                                .environmentObject(game)
                        }
                    }
                    VStack{
                        ForEach(game.ButtonAnswers, id: \.id){ answer in
                            ButtonRows(answer: answer)
                                .environmentObject(game)
                        }
                    }
                }

From left to right reading across the VStacks, my code gives 1, 1, 2, 2, 3, 3, etc. I want it to be 1, 2, 3, 4, 5, 6, etc. so that one VStack contains, 1, 3, 5 the other 2, 4, 6

Let me know if you have any suggestions on how to do this. Thank you!

CodePudding user response:

You can get an index by enumerating your answers in the ForEach loop, then use the index to decide whether or not to show that value or not.

ForEach(Array(game.ButtonAnswers.enumerated()), id: \.offset) { index, answer 

    if index&1 == 1 {        
        ButtonRows(answer: answer)
            .environmentObject(game)
    }
}

and then for the other column, use

 if index&1 == 0

a full example here:

import SwiftUI

struct GameBoard5View: View {
    struct Game {
        var ButtonAnswers: [String]
    }
    var game = Game(ButtonAnswers: ["hello", "Goodbye", "black", "white", "up", "down"])
    
    var body: some View {
        HStack {
            List {
                ForEach(Array(game.ButtonAnswers.enumerated()), id: \.offset) { index, answer in
                    if index&1 == 1 {
                        ButtonRows(answer: answer)
                    }
                }
            }
            List {
                ForEach(Array(game.ButtonAnswers.enumerated()), id: \.offset) { index, answer in
                    if index&1 == 0 {
                        ButtonRows(answer: answer)
                    }
                }
            }
        }
    }
    
    func  ButtonRows(answer: String)-> some View {
        return Text(answer)
    }
    
}
  • Related