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?

8
  • The comma is the delimiter used in Chart Js. So I have to keep them, without replacing the dots. Commented Feb 5, 2019 at 10:16
  • 1
    Well the answers below are the same as the answer here, you just need to get better at searching for answers rather than asking duplicated questions. Commented Feb 5, 2019 at 10:17
  • @IslamElshobokshy link goes to converting an array to integer. My question is for float and without replacing dots. It's different. Commented Feb 5, 2019 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 Commented Feb 5, 2019 at 10:28
  • @jo_va Chart Js accepts floats. It uses commas as delimiters and natively dots for decimals. Commented Feb 5, 2019 at 10:32

8 Answers 8

10

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)

Sign up to request clarification or add additional context in comments.

3 Comments

how about the minus number?
@sonictl add an optional minus -? at the beginning of the regex: /-?\d+(?:\.\d+)?/
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.
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) 

Comments

4

You could just map numbers.

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

console.log(numbers);

4 Comments

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.
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.
@JulioSouto, the question is, why does not accept chartjs this array? have you had try an array with just numbers?
@NinaScholz I have done it successfully with integers. The issue was restrictedly related to float.
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.

1 Comment

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.
1

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

console.log(num);

Number() mdn

1 Comment

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.
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)

2 Comments

Thank you, Islam for the time spent on trying to solve it. I have tested the solution presented by Adiga and it worked, so I have marked as solved.
@JulioSouto my pleasure!
0
var arr = ["6,35,2,72,11,79,183,25"]
var result=arr.map(Number);
result[]
typeof(result[])

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.