I have an array of Bools and want to edit an array of Scores and an array of Dates for the values that are False. I can't get to it. I thought of getting elements that are false and using that array to remove those elements from the score array but I can imagine there is a direct way of doing it.
let hbiCompleteArray = [true, true, true, true, false, true, true, false, false]
let hbiScoreArray = [12, 12, 12, 12, 3, 13, 13, 2, 2]
I want an array of completeHbiScores = [12, 12, 12, 12, 13, 13]
If you have to use two arrays, you can solve this with zip
, filter
and map
like this:
let hbiCompleteArray = [true, true, true, true, false, true, true, false, false]
let hbiScoreArray = [12, 12, 12, 12, 3, 13, 13, 2, 2]
let result = zip(hbiCompleteArray, hbiScoreArray).filter { $0.0 }.map { $1 }
print(result)
Gives:
[12, 12, 12, 12, 13, 13]
Explanation: the zip
interleaves the two arrays (makes an array of (Bool, Int)
tuples), then filter { $0.0 }
only keeps the true
booleans, then the map
only keeps the Int values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With