I'm currently working on a Vue method where I extract information from a WordPress database. The data retrieved sometimes contains unnecessary text that I want to filter out.
Using the prodInfo
variable, the input data looks something like this: 2,5kg meal
or 500g cookie
I find the regex I'm using to be cumbersome as it requires creating data with template literals. Is there a more efficient way to handle these calculations and extract the necessary information?
extractTotal(prodInfo, prodPrice, code){
let val = prodInfo.match(/[0-9]/g)
let multiplier
if( val.length > 2 ){
multiplier = `${val[0]}${val[1]}${val[2]}`
} else if( val.length === 1 ) {
multiplier = val[0]
} else {
multipier = `${val[0]}.${val[1]}`
}
if( multiplier === '500' ){
return (parseFloat(prodPrice) * this.q[code] / 2)
} else if( multiplier === '1' ) {
return (parseFloat(prodPrice) * this.q[code] * 1)
} else {
return (parseFloat(prodPrice) * this.q[code] * multiplier)
}
}