I have a JavaScript object with JSON data
var info = [
{
"MONTH":" 2018-01",
"DEPARTMENT":"Legals",
"EMPLOYEE":"Smith A.",
"AMOUNT":"14289.66"
},
{
"MONTH":" 2018-01",
"DEPARTMENT":"Legals",
"EMPLOYEE":"Jonson B.",
"AMOUNT":"7408.05"
},
{
"MONTH":" 2018-01",
"DEPARTMENT":"Legals",
"EMPLOYEE":"Lee C.",
"AMOUNT":"10102.98"
}
]
I am trying to calculate the total sum of the 'AMOUNT' property using the following function (taken from this source):
info.sum = function(data, property){
return data.reduce( function(prev, current){
return prev + parseFloat(current[property]);
}, 0);
};
totalAmount = info.sum(info, 'AMOUNT');
However, I encounter an error message: "TypeError: info.sum is not a function"
How can I correctly calculate the sum of 'AMOUNT' values within the object?