我有以下对象。
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"}
]
我想过滤上面的对象以获得:
A。我想要return Total based on my filters
(例如:如果我将名称指定为 ABC,部门指定为 First,FY 指定为 2016,季度指定为 1,月份指定为 April,那么应该filter/return
the Total i.e 100
对于给定的过滤器)
b.同样,我想return Sum of all the Totals
(例如:如果我将名称指定为 ABC,部门指定为 First,FY 指定为 2016 - 那么它应该return sum of the required Total values(i.e 100+150=250)
对于给定的FY 2016
only)
请帮我实现这个要求,我该如何实现,谢谢。
我在下面尝试过,但它给出了给定名称的所有结果(例如:如果我将名称指定为 ABC,那么它仅返回所有详细信息 ABC)
return getData().then(res => {
res.data.filter(customerDetails =>{
if(customerDetails.Name === name && customerDetails.FY === fy && customerDetails.Quarter === quarter && customerDetails.Month === month && customerDetails.Dept === dept)
agent.add(`Details: ${name}, Dept: ${customerDetails.Dept},
FY: ${customerDetails.FY}, Quarter: ${customerDetails.Quarter}, Month: ${customerDetails.Month},
Total: ${customerDetails.Total} `);
});
});