I have an array of 1000 elements, which elements fluctuate from 0 to 1.
I want to scan that array and zero all values below a certain threshold, let's say, 0.3
.
I know I can do something like
let filteredArrayOnDict = myArray.filter { $0 > 0.3}
and I will get a new array with the elements above 0.3
. But that is not what I want. I want to zero the elements below 0.3 and keep the resulting array with the same number of elements.
I can iterate over the array like
var newArray : [Double] = []
for item in myArray {
if item > 0.3 {
newArray.append(item)
} else {
newArray.append(0)
}
}
but I wonder if there is some more elegant method using these magical commands like filter, map, flatmap, etc.