3

如何对这样的数组求和 [ '', '4490449', '2478', '1280990', '22296892', '244676', '1249', '13089', '0', '0', '0\n' ]

如果我在那个数组上调用类似的东西['','4490449', ... , '0\n' ].reduce(function(t,s){ return t+s),那么这些刺就会被连接起来而不是相加。

我尝试了一些铸造,parseInt()但这会导致NaN:)

4

6 回答 6

8

您需要确保求和的值是整数。这是一种可能的解决方案:

var ary=[ '', '4490449', '2478', '1280990', '22296892', 
          '244676', '1249', '13089', '0', '0', '0\n' ];

console.log(
  ary
    .map( function(elt){ // assure the value can be converted into an integer
      return /^\d+$/.test(elt) ? parseInt(elt) : 0; 
    })
    .reduce( function(a,b){ // sum all resulting numbers
      return a+b
    })
)​;

它将“28329823”打印到控制台。

请参阅http://jsfiddle.net/hF6xv/上的小提琴

于 2012-10-29T20:02:20.000 回答
3

这似乎工作正常:

var arry = [ 'asdf', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'ham10am' ];

var res = arry.reduce(function(prev, curr){
    return (Number(prev) || 0) + (Number(curr) || 0);
});

console.log(res); // prints 45
于 2012-10-29T20:04:49.693 回答
1

试试这个:

var sum = 0,
    arr = [ '', '1', '2', '3.0','4\n', '0x10' ],
    i = arr.length;

while( i-- ) {
    // include radix otherwise last element gets interpreted as 16
    sum += parseInt( arr[i], 10 ) || 0; 
}

console.log( sum ) // sum => 10 as 3.0 and 4\n were successfully parsed

在这里提琴

于 2012-10-29T20:09:37.457 回答
1

您使用 parseInt 是正确的。

但是您需要为每个 reduce 参数使用它。

此外,您还需要检查每个 parseInt 的结果是否是一个数字,因为如果不是,该函数将尝试将一个数字与 NaN 相加,并且所有其他和最终也将是 NaN。

Mozilla关于 parseInt的 ECMAscript文档说:

如果第一个字符不能转换为数字,则 parseInt 返回 NaN。

然后,为了避免让 NaN 破坏你的目标,你可以像这样实现它:

function parseIntForSum(str) {
    var possibleInteger = parseInt(str);
    return isNaN(possibleInteger) ? 0 : possibleInteger;
}

function sum(f, s) {
    return parseIntForSum(f) + parseIntForSum(s);
}

window.alert('sum = ' + [ '', '4490449', '2478', '1280990', '22296892', '244676', '1249', '13089', '0', '0', '0\n' ].reduce(sum)​​​);​

这是一个 jsfiddle 工作:http: //jsfiddle.net/cLA7c/

于 2012-10-29T20:17:18.810 回答
0

您的数组中有一些不是整数的值。让我们假设它们都是,那么你可以这样做:

['4490449', '2478', '1280990', '22296892', '244676', '1249', '13089'].reduce( 
    function(t, s) { 
        return parseInt(t) + parseInt(s); 
    }
);
于 2012-10-29T19:59:09.247 回答
0
import java.util.List;

public class MixedSum {

 /*
  * Assume input will be only of Integer o String type
  */
  public int sum(List<?> mixed) {
    
    // A new array to store all the elements in the mixed list after converting them to integers
    int[] newIntList = new int[mixed.size()];
    
    // Variable to store sum of all elements in our new array
    int sum = 0;
    
    // Loop through the mixed list and convert all integers to strings and then back to string
    //(mixed.get(i) + "") converts all integers to strings and leave strings as strings
    // parseInt() function converts everything back to integers 
    for(int i = 0; i < mixed.size(); i++){
      newIntList[i] = Integer.parseInt(mixed.get(i) + "");
    }
    
    // Loops through the array and sum up all elements
    for(int i = 0; i < newIntList.length; i++){
      sum += newIntList[i];
    }
    
    //returns sum
    return sum;
  }
}
于 2021-03-12T12:43:20.353 回答