My following code needs to execute a alcohol percentage, this outputs like 43,000004 at some point so I'd like to trim my data to 43,0, 45,3 etc. But every time I execute any trim/parse functions from JavaScript, my browser freezes.
Here's my code:
incrementAlcohol() {
// Create a variable as an array.
let alcoholData = []
// Loop through the array until you achieve 40 till 95.
for (let i = 40; i <= 95; i + 0.1) {
// Split the first 3 integers in the string to remove the decimals.
parseFloat(i).toFixed(3)
// Parse it into a string for the autocomplete component.
i.toString()
// Push it into the array.
alcoholData.push({
'id': i,
'name': i + "%"
})
}
// Return the age.
return alcoholData
},
You could create a generator function that yields the current value and then wrap that in an array.
Also, the range should be inclusive/exclusive (max = end - step
).
const rangeGenerator = function*(start, end, step, fn) {
for (let val = start; val < end; val += step) {
yield fn ? fn(val) : val;
}
}
let alcoholData = [...rangeGenerator(40, 95, 0.1, (val) => {
return ((fixed) => ({ id : fixed, name : fixed + "%" }))(val.toFixed(3));
})]
console.log(alcoholData);
.as-console-wrapper { top: 0; max-height: 100% !important; }