简体   繁体   English

将数组中的数字相加

[英]add up numbers in array

I've been following this post but I cannot get my array to add up all numbers in my array. 我一直在关注这篇文章,但是我无法让我的数组将数组中的所有数字相加。

I use this: 我用这个:

 var array_new = [$(".rightcell.emphasize").text().split('€')];

to give me this array: 给我这个数组:

array_new: ,102.80,192.60,22.16

then I do this: 然后我这样做:

var sum = array_new.reduce((a, b) => a + b, 0);
  console.log(sum); //gives me this 0,102.80,192.60,22.16

When all I want to do is add up the numbers, I get this result 0,102.80,192.60,22.16. 当我要做的就是将数字加起来时,我得到的结果是0,102.80,192.60,22.16。 Can anyone advise me? 谁能告诉我?

Since your array is composed of undefined and a bunch of strings you have to parse the values to get the numbers. 由于数组由undefined的字符串和一堆字符串组成,因此您必须解析值以获取数字。 The answer would be: 答案将是:

 var data = [,'102.80','192.60','22.16']; console.log(data.reduce((r,c) => r + parseFloat(c), 0)) 

However if you do not want to deal with the parsing in that function you can make sure that your array comes out as array of numbers like this: 但是,如果您不想在该函数中进行解析,则可以确保将数组显示为数字数组,如下所示:

Array.from([$(".rightcell.emphasize").text().split('€')], (x) => parseFloat(x || 0))

Which would get your array ready for summation and without the need to parse inside the Array.reduce . 这将使您的数组准备好求和,而无需在Array.reduce内部进行解析。 So it would be something like this: 所以会是这样的:

 var strings = [,'102.80','192.60','22.16']; var numbers = Array.from(strings, (x) => parseFloat(x || 0)) console.log(numbers.reduce((r,c) => r + c, 0)) 

But in your case it would be shorter since you would do the first 2 lines as one as shown in the 2nd code snippet. 但是根据您的情况,它会更短一些,因为您将前两行作为第二行代码片段中所示的那样进行。

That's because your original array is a string array. 那是因为您的原始数组是一个字符串数组。

Use parseFloat to create numbers from the strings. 使用parseFloat从字符串创建数字。

var strArr = ['102.80','192.60','22.16'];
var sum = strArr.reduce((a,b) => a + parseFloat(b),0); // 317.56

Your elements in the array are treated as strings, therefore the + in your reduce function concatenates the array elements into another string. 数组中的元素被视为字符串,因此reduce函数中的+将数组元素连接到另一个字符串中。 Try to use parseFloat() to treat them as numbers: 尝试使用parseFloat()将它们视为数字:

var sum = array_new.reduce((a, b) => a + parseFloat(b), 0);

EDIT: Thanks to charlietfl who mentioned, that parseFloat(a) is redundant, fixed it. 编辑:感谢charlietfl提到的, parseFloat(a)是多余的,对其进行了修复。

EDIT: (since you already deleted your comment, here would be the solution for your problem): in your case this solution won't work, because one of your array elements is "" (an empty string) which can't be treated as a number, so you could map your array values before you try to add them up: 编辑:(因为您已经删除了注释,这将是您问题的解决方案):在您的情况下,此解决方案将不起作用,因为您的数组元素之一是“”(空字符串),无法处理作为一个数字,因此您可以先映射数组值,然后再尝试将其相加:

array_new.map(a => (a == "") ? "0" : a);

in javascript, you can concat numbers by + or strings as well . 在javascript中,您也可以用+或字符串连接数字。 I will give you an example : 我举一个例子:

var x = "2" + "1"; // x = 21
var x = 2 + 1 ; // x = 3

You can look to this . 你可以看看这个

You should convert your array to integers (parseInt) or floats(parseFloat) in this function array_new.reduce((a, b) => a + b, 0); 您应该在此函数中将数组转换为整数(parseInt)或floats(parseFloat)array_new.reduce((a,b)=> a + b,0); It will be something like 它会像

array_new.reduce((a, b) => a + parseInt(b), 0);//or
array_new.reduce((a, b) => a + parseFloat(b), 0);

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

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