anoop chandran anoop chandran - 2 months ago 6
Javascript Question

Finding total sum of JS array and excluding all NULL values

Assuming that I have a js object array with my monthly expense data like this

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


the dotted lines denote the data for other days, and as you can see some of the days has bill amount denoted as integers and somedays it has value of null. I could use zero, but assuming i got null values in my array and i want to find total sum of expenses of a month, how do i write the function? Check this jsfiddle

Answer

There are several problems in your existing code:

  1. response.bills should be response[i].bills (in two places)
  2. The totalSum variable should be declared before the loop and initialised to 0, not to an empty string.
  3. The values in your data are strings, so you need to convert them to numbers before trying to add them up.

Here's an updated version that fixes those issues:

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

But given that Number(null) returns 0, you don't need the if:

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

Or use .reduce():

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

Working demo:

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