2

I have a string named :

graph_data =[16.665,0,16.665,19.23,35.79,16.665,31.71,16.665,0,16.665,0,16.665,16.665,41.615]

I wanted to covert it to array of number. I am using the following code:

var graph_data1=new Array();
var graph_data = graph_data.split(",");
for (var i=0; i<graph_data.length; i++)
{
graph_data1[i] = parseFloat(graph_data[i]);
}

the output of graph_data1 is showing following NaN,0,16.665,19.23,35.79,16.665,31.71,16.665,0,16.665,0,16.665,16.665,41.615

can any one tell me why it is adding Nan and 0

thanks in advance .

3
  • if your string is indeed a string, the first element after split would be [16.665 which is NaN ... your second element is 0, which is why you get a 0 Commented Aug 4, 2015 at 10:02
  • Check demo Commented Aug 4, 2015 at 10:05
  • Does your string actually contain graph_data=? Commented Aug 4, 2015 at 10:10

6 Answers 6

7

You could do this quite easily with JSON.parse

var myArray = JSON.parse('[16.665, 0, 16.665, 19.23, 35.79, 16.665, 31.71, 16.665, 0, 16.665, 0, 16.665, 16.665, 41.615]')

and if you want to do it all at one shot a replace to remove the content before [

var myArray = JSON.parse('graph_data = [16.665, 0, 16.665, 19.23, 35.79, 16.665, 31.71, 16.665, 0, 16.665, 0, 16.665, 16.665, 41.615]'.replace(/.*\[/,'['))
Sign up to request clarification or add additional context in comments.

4 Comments

Not if the string begins with graph_data = you couldn't.
good point! A substring or replace should fix that. editing in a bit.
damn, way faster than me :<
Little annoyed with myself that I didn't think of this, my substring answer below is looking a little stupid right now :)
0

Issue is that your code is trying to parse the "[" character as a number use substring to get around this like below:

var graph_data="[16.665,0,16.665,19.23,35.79,16.665,31.71,16.665,0,16.665,0,16.665,16.665,41.615]";
var graph_data1=[];
var graph_data = graph_data.substring(1, graph_data.length - 2).split(",");
for (var i=0; i<graph_data.length; i++)
{
graph_data1[i] = parseFloat(graph_data[i]);
}

Comments

0

When you are using

graph_data.split(',');

it is taking the first number as

[16.665

you need to remove [ from your number you can do that by simply doing

graph_data1[i] = parseFloat(graph_data[i].replace('[',''));

although this is not good practice but it will do the job for you.

Comments

0

Much easier, just json parse from the start to the end of the array:

var myString = "graph_data =[16.665,0,16.665,19.23,35.79,16.665,31.71,16.665,0,16.665,0,16.665,16.665,41.615]";
var startInd = myString.indexOf('[');
var endInd = myString.indexOf(']')+1; //+1 to include the ]
var myIntArr = JSON.parse(myString.substring(startInd, endInd));
console.log(myIntArr) // [16.665,0,16.665,19.23,35.79,...]

Comments

0

You can try below code:

graph_data='[16.665,0,16.665,19.23,35.79,16.665,31.71,16.665,0,16.665,0,16.665,16.665,41.615]'
var regexp = /(\+|-)?((\d+(\.\d+)?)|(\.\d+))/gi;
var matches_array = graph_data.match(regexp);
graph_data_array = matches_array.map(function (t) {
        return t;
    });
console.log(graph_data_array)

Comments

0

Use JSON.parse instead

graph_data  ="[16.665,0,16.665,19.23,35.79,16.665,31.71,16.665,0,16.665,0,16.665,16.665,41.615]"

graph_data = JSON.parse(graph_data)

console.log(graph_data)
=> [16.665, 0, 16.665, 19.23, 35.79, 16.665, 31.71, 16.665, 0, 16.665, 0, 16.665, 16.665, 41.615]

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.