score:6

Accepted answer

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)

score:0

map over the array with the number function, it will handle the conversion:

console.log(['6.35', '2.72', '11.79', '183.25'].map(number));

if you want commas in your numbers, then you must stick with a string representation.

see this so answer about a similar problem with chartjs.

score:0

var arr = ["6,35,2,72,11,79,183,25"]
var result=arr.map(number);
result[]
typeof(result[])

score:0

i was having the same problem this is a solution i found i had

x = "11,1.1,100,100,2,3333,99"

and i wanted

x = [11,1.1,100,100,2,3333,99]

here's my solution

x.tostring().replace(/, +/g, ",").split(",").map(number)

score:1

var num = ['6.35', '2.72', '11.79', '183.25'].map(num => number(num));

console.log(num);

number() mdn

score:1

parse the values to float :

console.log(['6.35', '2.72', '11.79', '183.25'].map(i => parsefloat(i)));

if for some reason .map() doesn't work just use a loop :

var array = ['6.35', '2.72', '11.79', '183.25']
var x = 0;
var len = array.length
while(x < len){ 
    array[x] = parsefloat(array[x]); 
    x++
}

console.log(array)

score:4

you could just map numbers.

var array = ['6.35', '2.72', '11.79', '183.25'],
    numbers = array.map(number);

console.log(numbers);

score:5

\d matches only digits, it's the short for [0-9]. for example, in 6.35 \d+ matches 6 and then 35 separately and the dot is ignored. what you get in result is array containing those matches.

as suggested in other answers, use of match is redundant in your case and you can go with:

array.map(number) 

Related Query

More Query from same tag