Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property 'reduce' of undefined at getTotal() in javascript [duplicate]

From the below code, I want to get the value of Total based on my input values/filters. If I use .filter() method it is not working in my application(getting empty response or 0), but working in externally standalone page, I am not sure why. Please help me. Thanks.

With the below code, I am getting the error in my application: TypeError: Cannot read property 'reduce' of undefined at getTotal()

index.js:

function getTotal(data, filters) {

console.log(JSON.stringify(filters));//{"Name":"ABC","Dept":"First","FY":"2016","Quarter":"1","Month":"April"} 
console.log(JSON.stringify(data));//[{"Name":"ABC","Dept":"First","FY":"2016","Quarter":"1","Month":"April","Total":"1200"},{"Name":"ABC","Dept":"Second","FY":"2017","Quarter":"2","Month":"May","Total":"200"},{"Name":"ABC","Dept":"First","FY":"2016","Quarter":"1","Month":"June","Total":"150"},{"Name":"DEF","Dept":"First","FY":"2016","Quarter":"1","Month":"April","Total":"200"},{"Name":"DEF","Dept":"Second","FY":"2017","Quarter":"2","Month":"May","Total":"100"},{"Name":"DEF","Dept":"First","FY":"2016","Quarter":"1","Month":"June","Total":"500"}] 

    var f = Object.entries(filters);
    console.log("f is: "+f);// Name,ABC,Dept,First,FY,2016,Quarter,1,Month,April
    const test = data.find(o => f.every(([k, v]) => o[k] === v)).reduce((s, { Total }) => s + +Total, 0);
    return test;//Output should be: 100
}


var data = [{ Name: "ABC", Dept: "First", FY: "2016", Quarter: "1", Month: "April", Total: "100" }, { Name: "ABC", Dept: "Second", FY: "2017", Quarter: "2", Month: "May", Total: "200" }, { Name: "ABC", Dept: "First", FY: "2016", Quarter: "1", Month: "June", Total: "150" }, { Name: "DEF", Dept: "First", FY: "2016", Quarter: "1", Month: "April", Total: "200" }, { Name: "DEF", Dept: "Second", FY: "2017", Quarter: "2", Month: "May", Total: "100" }, { Name: "DEF", Dept: "First", FY: "2016", Quarter: "1", Month: "June", Total: "500" }];

console.log(getTotal(data, { Name: 'ABC', Dept: 'First', FY: '2016', Quarter: '1', Month: 'April' })); // shoud give: 100

Yes, got the answer now at: How to return and get the sum of object properties based on given filters?

like image 825
Guna Avatar asked Mar 19 '26 16:03

Guna


1 Answers

You should use Array.prototype.filter instead of Array.prototype.find as it returns an array instead of a single object from your array. Objects don't have a reduce method arrays do:

function getTotal(data, filters) {
   var f = Object.entries(filters);
   const test = data.filter(o => f.every(([k, v]) => o[k] === v)).reduce((s, { Total }) => s + +Total, 0);
   return test;//Output should be: 100
}


var data = [{ Name: "ABC", Dept: "First", FY: "2016", Quarter: "1", Month: "April", Total: "100" }, { Name: "ABC", Dept: "Second", FY: "2017", Quarter: "2", Month: "May", Total: "200" }, { Name: "ABC", Dept: "First", FY: "2016", Quarter: "1", Month: "June", Total: "150" }, { Name: "DEF", Dept: "First", FY: "2016", Quarter: "1", Month: "April", Total: "200" }, { Name: "DEF", Dept: "Second", FY: "2017", Quarter: "2", Month: "May", Total: "100" }, { Name: "DEF", Dept: "First", FY: "2016", Quarter: "1", Month: "June", Total: "500" }];

console.log(getTotal(data, { Name: 'ABC', Dept: 'First', FY: '2016', Quarter: '1', Month: 'April' })); // shoud give: 100
like image 103
Fullstack Guy Avatar answered Mar 21 '26 04:03

Fullstack Guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!