2

I have a string as such:

string = "[x,y,z]"

Where x, y and z are valid javascript floats as strings. Some examples:

-0.9999
1.
1.00000000000E-5
-1E5

What is the most efficient (fastest) way to parse this string into an actual javascript array of floats without using Eval?

Now I do this:

parseFloatArray = function(string){
    // isolate string by removing square brackets
    string = string.substr( 1, string.length-2 )

    // create array with string split
    var array = string.split(',');

    // parse each element in array to a float
    for (var i = 0, il = array.length; i < il; i++){
        array[i] = parseFloat(array[i]);
    }

    // return the result
    return array
}

It is important that the solution works correctly for the above examples.

I also tried with JSON.parse which seemed perfect at first, but it returns a SyntaxError for the second example 1. where there is nothing following the decimal separator.

I prepared a fiddle for testing.

11
  • 1
    @ADreNaLiNe-DJ @Jamiec 1. is a perfectly valid float Commented May 27, 2016 at 8:50
  • 1
    @zerkms I sort of agree, in that parseFloat will accept it, but in no number system can a decimal place exist without a numeric after it. This problem is XY - fix whatever is producing an invalid(ish) string representing an array (or make it return an array not a string, for that matter!). Commented May 27, 2016 at 8:52
  • 1
    1..toSource() is valid JS code where 1. is number and .toSource() is its method. Commented May 27, 2016 at 8:53
  • 1
    @Cyril Nice one :) Commented May 27, 2016 at 8:56
  • 1
    @Jamiec Yes, if you have method toSource() then it works. You can try other methods, e.g. 1..valueOf(). Commented May 27, 2016 at 8:59

5 Answers 5

2

Instead of this

array[i] = parseFloat(array[i]);

try array[i] = +array[i];

Above handles all the test cases pretty well.

Here is the working fiddle

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

1 Comment

Would be interesting to see some numbers on the performance of this compared to parseFloat and Number. jsPerf is down for some time already, I should maybe do it manually.
1

Try this :

str = "[-0.9999, 1., 1.00000000000E-5,-1E5]";
str.slice(1, str.length-1).split(',').map(Number);
// [-0.9999, 1, 0.00001, -100000]

Comments

0

parseFloat basic syntax is parseFloat(string). https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

If you think the input values are only numbers you can use Number(x) rather than parseFloat.

Also, you might get different values upon parsing because all floating-point math is based on the IEEE [754] standard. so use toPrecision() method to customize your values- https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Number/toPrecision.

Comments

0

Instead of you writing the for loop, you can always use the javascript built in array.prototypes

var string = "[1.,-0.9999,-1.00000E-5]";
var array = string.substr( 1, string.length-2 ).split(',');

console.log(array.map(function(i){return parseFloat(i);}));

you can also use the unary operator instead of parseFloat().

console.log(array.map(function(i){return +i;}));

2 Comments

I think array.map( parseFloat ); would be enough, you don't have to wrap it in a function.
Yes. That is the short form of what I wrote. Internally this is process array.map(function(i){return parseFloat(i);})
-1

Most efficient to extract is replacing and splitting.

var str = "[312.413,436.6546,34.1]"; // string

str = () => str.replace(/\[|\]/g, "").split(","); //[312.413, 4...] Array

most eficient to parse is just preceed the string with a "+", that will numerify it and, since javascript works with floats as primitives (and not integers), they will be automatically parsed.

var a = "1.00000000000E-5";
console.log( +a ); // 0.00001

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.