6

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?

adiga
  • 34,372
  • 9
  • 61
  • 83
Julio S.
  • 944
  • 1
  • 12
  • 26
  • The comma is the delimiter used in Chart Js. So I have to keep them, without replacing the dots. – Julio S. Feb 05 '19 at 10:16
  • 1
    Well the answers below are the same as the answer [here](https://stackoverflow.com/a/35766937/9942418), you just need to get better at searching for answers rather than asking duplicated questions. – Alexandre Elshobokshy Feb 05 '19 at 10:17
  • @IslamElshobokshy link goes to converting an array to integer. My question is for float and without replacing dots. It's different. – Julio S. Feb 05 '19 at 10:20
  • A float is a float, the dot has nothing to do with it, when you use Number to convert the string to Numbers, you do get floats, if Chart JS does not accept floats, this is another problem and you should reformulate your question so we can better help you – jo_va Feb 05 '19 at 10:28
  • @jo_va Chart Js accepts floats. It uses commas as delimiters and natively dots for decimals. – Julio S. Feb 05 '19 at 10:32
  • The answers below do return dots, but if they use commas as delimiters then they're using strings and not actual numbers. None of the answers below worked for you? If none did, then your question isn't really the question you need to ask and you should search for the actual problem. – Alexandre Elshobokshy Feb 05 '19 at 10:33
  • See this: https://stackoverflow.com/a/26567557/9325419 – jo_va Feb 05 '19 at 10:35
  • As @Islam Elshobokshy said, by converting to numbers, you will only get dots, if you want to have commas, then you have no choice but to stick with strings – jo_va Feb 05 '19 at 10:36

8 Answers8

9

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)
adiga
  • 34,372
  • 9
  • 61
  • 83
  • how about the minus number? – sonictl Apr 21 '20 at 08:50
  • @sonictl add an optional minus `-?` at the beginning of the regex: `/-?\d+(?:\.\d+)?/` – adiga Apr 21 '20 at 10:10
  • Thanks a lot! I suggest modify the answer into the comment like regex to allow it for all the real numbers. since the 'float' in this question should be containing all real numbers. – sonictl Apr 22 '20 at 12:58
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) 
loler
  • 2,594
  • 1
  • 20
  • 30
4

You could just map numbers.

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

console.log(numbers);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • If I use just this method, then Chart Js (where the array is used) does not render the chart. Also, this method itself, for some reason fails, so I cannot even print the result. – Julio S. Feb 05 '19 at 10:18
  • there's no need to convert number content to number literals in JavaScript because that's why we love JavaScript - it's alive! If we serve them well, id does it for us. – Bekim Bacaj Feb 05 '19 at 10:18
  • @JulioSouto, the question is, why does not accept chartjs this array? have you had try an array with just numbers? – Nina Scholz Feb 05 '19 at 10:25
  • @NinaScholz I have done it successfully with integers. The issue was restrictedly related to float. – Julio S. Feb 05 '19 at 10:47
2

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.

jo_va
  • 13,504
  • 3
  • 23
  • 47
  • As stated above, if I use just this method, then Chart Js (where the array is used) does not render the chart. Also, this method itself, for some reason fails, so I cannot even print the result. – Julio S. Feb 05 '19 at 10:18
1

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

console.log(num);

Number() mdn

Alexandre Elshobokshy
  • 10,720
  • 6
  • 27
  • 57
Ankit Sinha
  • 1,630
  • 1
  • 20
  • 19
  • Guys, this gives me the following error: TypeError: window.column_b.map is not a function. (In 'window.column_b.map(i => Number(i))', 'window.column_b.map' is undefined). PS: "window.column_b" because the it's a global variable declared outside the function. – Julio S. Feb 05 '19 at 10:34
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)
Alexandre Elshobokshy
  • 10,720
  • 6
  • 27
  • 57
0
var arr = ["6,35,2,72,11,79,183,25"]
var result=arr.map(Number);
result[]
typeof(result[])
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)