# How to calculate the average value of non-zero elements

We have an array of 12 elements, there are elements with numerical values and there are elements with zeros. How to calculate the average value of non-zero elements.

Is the element from which you need to get the average value by skipping zeros

``````for(var i in result){
if( i === 'Office 1'){
result[i].forEach(x=>{
x.Monthly_Expense__r.forEach(y=>{
office1.push({
key : nameMonths[new Date(y.MonthDate__c).getMonth()],
intMon : new Date(y.MonthDate__c).getMonth(),
value : y.Balance__c,
})
})
})
}
}
``````

That's what I'm trying.

``````for(var i = 0; i < office1.length; i ++){
console.log(office1[i]);
averageValueOffice1 = totalOffice1 / i;
}
``````

In total, I get the average number divided by 12, and the elements with values in this array are only 2

This my code now

``````for(let i = 0; i < office1.length; i ++){
averageValueOffice1 = office1[i].value;
console.log(averageValueOffice1);
let nonZeroNums = averageValueOffice1.filter(item => (isFinite(item) && item!==0));
let total = nonZeroNums.reduce((accumulator, currentValue) => accumulator + currentValue);
let avg = total/nonZeroNums.length;

}
``````

If you comment on the code like this, here's what I get in console.log If you remove the comments

But here's my method, which works!!!!

``````var countOfExistBalances3 = 0;
var resultAverage3 = office3.reduce(function(sum, curr) {
if (!!curr.value) countOfExistBalances3++;
return sum + curr.value;
}, 0);
var resultAverageOffice3 = resultAverage3 / countOfExistBalances3;
``````

Here is the code with explanation:

``````// declare your array
let myarr = [{"key":"January","intMon":0,"value":0},{"key":" February","intMon":1,"value":0},{"key":" March","intMon":2,"value":0},{"key":" April","intMon":3,"value":0},{"key":" May","intMon":4,"value":0},{"key":" June","intMon":5,"value":0},{"key":" July","intMon":6,"value":0},{"key":" August","intMon":7,"value":0},{"key":" September","intMon":8,"value":0},{"key":" October","intMon":9,"value":400},{"key":" November","intMon":10,"value":434},{"key":" December","intMon":11,"value":0}]

// collect numbers and non-zero numbers
let nonZeroNums = myarr.map(item => item.value).filter(item => (isFinite(item) && item!==0));
console.log(nonZeroNums); // [400, 434]

// Get total of numbers
let total = nonZeroNums.reduce((accumulator, currentValue) => accumulator + currentValue);

// calculate average
let avg = total/nonZeroNums.length;

console.log('avg ', avg); // 417
``````

Refs:
1. isFinite
2. Reduce

• It doesn't work with my array. Oct 17, 2019 at 11:00
• @GariVudi, can you copy paste the array in question. You can get the JSON by using console.log(JSON.stringify(myArray)) Oct 17, 2019 at 12:59
• This my json.stringify log([{"key":"January","intMon":0,"value":0},{"key":" February","intMon":1,"value":0},{"key":" March","intMon":2,"value":0},{"key":" April","intMon":3,"value":0},{"key":" May","intMon":4,"value":0},{"key":" June","intMon":5,"value":0},{"key":" July","intMon":6,"value":0},{"key":" August","intMon":7,"value":0},{"key":" September","intMon":8,"value":0},{"key":" October","intMon":9,"value":400},{"key":" November","intMon":10,"value":434},{"key":" December","intMon":11,"value":0}]) Oct 17, 2019 at 13:09
• But I'm getting numbers in this method: for (var i = 0; i < office1.length; i++) { var item = office1[i].value; Oct 17, 2019 at 13:12
• @GariVudi modified answer. no need of any for loops. Oct 17, 2019 at 13:16