Search code examples
javascriptnode.jsexpressmathjs

Converting a string of comma separated numbers into numbers


I am trying get a list of comma separated numbers to process in math.js library (https://github.com/josdejong/mathjs/blob/master/docs/functions.md) in Node.js.

While I got the list list of numbers like "2,4,5" and did a comma split to store in an array. The library refused to process my array using it's math.add() function. It needs individual integers to process. I could use the function with array[0],array[1] but I want to provide all the integers at once using a single variable.

exports.add = function(req, res) {
    var v = req.params.v;
    var array = v.split(",");
    var intarray = [];
    for(var i=0; i<array.length; i++) {
        intarray[i] = +array[i];
    }
    var result = math.add(intarray);
    res.end(result.toString());
}

Solution

  • Update: You've said you want to call math.add once, with all the values, but I don't think it supports doing that. Both the doc on the function and the source say it only supports two arguments, not an unlimited series of them.

    So you can't do it all at once; you'd be better off doing it as you parse:

    exports.add = function(req, res) {
        var v = req.params.v;
        var array = v.split(",");
        var result = 0;
        for(var i=0; i<array.length; i++) {
            result = math.add(result, +array[i]);
        }
        res.end(result.toString());
    }
    

    Or in an ES5-enabled (or a shimmed) environment:

    exports.add = function(req, res) {
        var result = 0;
        req.params.v.split(",").forEach(function(val) {
            result = math.add(result, val);
        });
        res.end(result.toString());
    }
    

    But since you know these are JavaScript numbers, I don't see any reason to use math.add at all. It just adds them together, allowing for various types. But you know the type of these numbers, so you're not getting anything except overhead by calling it.

    Original answer before I went looking:


    It needs individual integers to process. I could use the function with array[0],array[1] but I want to provide all the integers at once using a single variable.

    If math.add supports a series of discrete arguments, you can use your array like this:

    var result = math.add.apply(math, intarray);
    

    apply calls the function that you call it on using the first argument you give it as the this value within the call, and the contents of the array you give as the second argument as the individual arguments for the function.

    It's easier to understand with an example:

    /// A function accepting three formal arguments
    function foo(a, b, c) {
        console.log("a = " + a);
        console.log("b = " + b);
        console.log("c = " + c);
    }
    
    // An array with 1, 2, 3 in it
    var a = [1, 2, 3];
    
    // Call `foo`, passing in the array to use as the discrete arguments
    foo.apply(undefined, a);
    

    That outputs:

    a = 1
    b = 2
    c = 3

    In that example, I didn't need this to be anything in particular within the call to foo, so I used undefined. When calling an object's method (math.add, for instance), it's best to use the object, as that's what this would normally be within the call.