简体   繁体   English

将一串用逗号分隔的数字转换为数字

[英]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. 我正在尝试获取要在Node.js中的math.js库( https://github.com/josdejong/mathjs/blob/master/docs/functions.md )中处理的逗号分隔数字的列表。

While I got the list list of numbers like "2,4,5" and did a comma split to store in an array. 当我得到数字列表时,例如“ 2,4,5”,并进行逗号分割以存储在数组中。 The library refused to process my array using it's math.add() function. 该库拒绝使用math.add()函数处理我的数组。 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. 我可以将函数与array [0],array [1]一起使用,但我想使用一个变量一次提供所有整数。

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());
}

Update : You've said you want to call math.add once, with all the values, but I don't think it supports doing that. 更新 :您已经说过要使用所有值一次调用math.add ,但是我不认为它支持这样做。 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: 或在启用ES5(或匀场)的环境中:

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. 但是,由于您知道这些是JavaScript数字,因此我完全看不出使用math.add任何理由。 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. 我可以将函数与array [0],array [1]一起使用,但我想使用一个变量一次提供所有整数。

If math.add supports a series of discrete arguments, you can use your array like this: 如果math.add支持一系列离散参数,则可以这样使用数组:

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. apply使用调用时使用的第一个参数作为this值,并将使用第二个参数给出的数组内容作为该函数的单独参数,调用您调用的函数。

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 . 在这个例子中,我并不需要this调用内尤其是任何foo ,所以我用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. 调用对象的方法(例如math.add )时,最好使用该对象,因为this通常在调用中。

math.js now supports the function sum , counting an array or matrix with values: math.js现在支持函数sum ,使用值对数组或矩阵进行计数:

var math = mathjs();

var result = math.sum([2,4,5]);
console.log(result); // 11

Use Array.prototype.reduce 使用Array.prototype.reduce

var result = intarray.reduce(function(prev, curr) { return math.add(prev, curr); }, 0);

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce for details on Array Reduce. 有关Array Reduce的详细信息,请参见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM