javascript - 类型错误 : Cannot read property 'reduce' of undefined at getTotal() in javascript

标签 javascript ecmascript-6 find reduce

<分区>

从下面的代码中,我想根据我的输入值/过滤器获取 Total 的值。如果我使用 .filter() 方法,它在我的应用程序中不起作用(得到空响应或 0),但在外部独立页面中工作,我不确定为什么。请帮我。谢谢。

使用以下代码,我的应用程序出现错误: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

是的,现在得到答案:How to return and get the sum of object properties based on given filters?

最佳答案

您应该使用 Array.prototype.filter 而不是 Array.prototype.find 因为它返回一个 array 而不是来自的单个对象你的阵列。 对象没有 reduce 方法,数组有:

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

关于javascript - 类型错误 : Cannot read property 'reduce' of undefined at getTotal() in javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57944715/

相关文章:

javascript - 在 JavaScript 中生成一个不重复的随机数

javascript - 这是什么JS模块

javascript - 根据 redux 状态中的另一个值解构对象

java - 找到并单击按钮 Java Robot

php - 使用sed命令批量替换字符串的特殊字符

c++ - 我正在尝试获取数字的随机顺序 1 :16 using srand and rand. 代码按预期工作,但仅进行到第 12 次迭代。为什么?

javascript - 获取单选按钮之前的状态点击事件/取消选中单选按钮(如果之前已选中)

javascript - 访问没有点和一对括号的变量(parent.child.grandchild)的孙子

ecmascript-6 - lodash 中的 stub 函数有什么意义?

javascript - ES6 代理的主要用例