I have this array and it is formatted as string:
['6.35', '2.72', '11.79', '183.25']
The problem is that when I convert it to numbers (using - without double quotes )
array.match(/\d+/g).map(Number) || 0;
it changes the dots used for decimals to commas. Then I end up with this new array:
6,35,2,72,11,79,183,25
So, instead of having 4 items inside the array, now I have 8 items, as my delimiters are commas.
Any ideas of how I can convert this array without replacing the dots?
Assuming you have an array in a string format, you can use the following regex to match all the decimals and then use .map(Number)
const str = "['6.35', '2.72', '11.79', '183.25']",
array = str.match(/\d+(?:\.\d+)?/g).map(Number)
console.log(array)
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