查找JS数组的总和并排除所有NULL值

时间:2016-09-29 04:33:46

标签: javascript arrays

假设我有一个带有我的月费用数据的js对象数组

var data=[
    {
    "date": "2016-09-01",
    "bills": "34"            
    },{
    "date": "2016-09-02",
    "bills": "34" 
    },{
    "date": "2016-09-03",
    "bills": null
    },{
    "date": "2016-09-04",
    "bills": "34"
    },{
    "date": "2016-09-05",
    "bills": null
    },{
    "date": "2016-09-06",
    "bills": "34"            
    },{
    "date": "2016-09-07",
    "bills": "34" 
    },{
    "date": "2016-09-08",
    "bills": null
    },{
    "date": "2016-09-09",
    "bills": "34"
    },{
    "date": "2016-09-10",
    "bills": null
    }
   ];

  var totalAmount = function(response){        
    for(var i=0; i<response.length; i++ ){
      if (response.bills!==null){
      var totalSum = "";
      totalSum += response.bills;
      }
    }
    return totalSum;
  };
  totalAmount(data);

虚线表示其他日子的数据,正如您所看到的,有些日子的账单金额表示为整数,有一天它的值为null。我可以使用零,但假设我的数组中有空值,我想找到一个月的总费用,我该如何编写函数?请检查此jsfiddle

3 个答案:

答案 0 :(得分:1)

现有代码存在以下几个问题:

  1. response.bills应为response[i].bills(在两个地方)
  2. totalSum变量应该在循环之前声明并初始化为0,而不是空字符串。
  3. 您的数据中的值是字符串,因此您需要在尝试添加它们之前将它们转换为数字。
  4. 以下是修复这些问题的更新版本:

    var totalAmount = function(response){
      var totalSum = 0;      
      for(var i=0; i<response.length; i++ ){
        if (response[i].bills!==null){
          totalSum += Number(response[i].bills);
        }
      }
      return totalSum;
    };
    

    但鉴于Number(null)返回0,您不需要if

    var totalAmount = function(response){
      var totalSum = 0;      
      for(var i=0; i<response.length; i++ ){
        totalSum += Number(response[i].bills);
      }
      return totalSum;
    };
    

    或使用.reduce()

    var totalAmount = function(response){
      return response.reduce(function(p, c) { return p + Number(c.bills); }, 0);      
    };
    

    工作演示:

    var data=[
        {
        "date": "2016-09-01",
        "bills": "34"            
        },{
        "date": "2016-09-02",
        "bills": "34" 
        },{
        "date": "2016-09-03",
        "bills": null
        },{
        "date": "2016-09-04",
        "bills": "34"
        },{
        "date": "2016-09-05",
        "bills": null
        },{
        "date": "2016-09-06",
        "bills": "34"            
        },{
        "date": "2016-09-07",
        "bills": "34" 
        },{
        "date": "2016-09-08",
        "bills": null
        },{
        "date": "2016-09-09",
        "bills": "34"
        },{
        "date": "2016-09-10",
        "bills": null
        }
       ];
    
    var totalAmountLoopIf = function(response){
      var totalSum = 0;      
      for(var i=0; i<response.length; i++ ){
        if (response[i].bills!==null){
          totalSum += Number(response[i].bills);
        }
      }
      return totalSum;
    };
    
    var totalAmountLoop = function(response){
      var totalSum = 0;      
      for(var i=0; i<response.length; i++ ){
        totalSum += Number(response[i].bills);
      }
      return totalSum;
    };
    
    var totalAmountReduce = function(response){
      return response.reduce(function(p, c) { return p + Number(c.bills); }, 0);      
    };
    
    console.log(totalAmountLoopIf(data));
    console.log(totalAmountLoop(data));
    console.log(totalAmountReduce(data));

答案 1 :(得分:0)

修改你的功能如下:

var totalAmount = function(response){
        var totalSum = 0;
  for(var i=0; i<response.length; i++ ){
    if (response[i].bills!==null){

      totalSum += parseFloat(response[i].bills);
    }
  }
  return totalSum;
};
每次循环开始时都会重新创建

totalSum并且您正在丢失计数。在循环开始之前声明它。账单也是一个字符串。在将其添加到sum之前,需要将其转换为数字。 response变量也是一个数组。您正在访问其中的值,就像对象一样。

答案 2 :(得分:0)

如果您想计算特定月份和年份,请尝试此操作,因为您的数据也可能包含其他月份:

var totalAmount = function(response,forYear,forMonth){
   var totalSum = 0;

   for(var i=0; i<response.length; i++ ){
       if (response[i].bills!==null){
           var d = new Date(response[i].date);
           if(forYear== d.getFullYear() && forMonth==d.getMonth())
               totalSum += parseInt(response[i].bills);
       }
    }
   return totalSum;
};
alert(totalAmount(data,2016,8)); //note Jan=0,Feb=1