如何根据给定的过滤器返回并获取对象属性的总和? [英] How to return and get the sum of object properties based on given filters?

查看:63
本文介绍了如何根据给定的过滤器返回并获取对象属性的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下对象.

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"}
]

我要过滤abve对象以获得:

I want to filter on the abve object to get:

a.我想return Total based on my filters(例如:如果我将Name设为ABC,Dept设为First,FY设为2016,Quarter设为1,Month设为April,那么对于给定的过滤器,它应该filter/return Total i.e 100)

a. I want to return Total based on my filters(ex: If I give Name as ABC, Dept as First, FY as 2016, Quarter as 1, Month as April, then it should filter/return the Total i.e 100 for the given filters)

b.同样,我想return Sum of all the Totals(例如:如果我将Name命名为ABC,Dept命名为First,FY命名为2016-那么对于给定的FY 2016,它应该return sum of the required Total values(i.e 100+150=250))

b. Similarly, I want to return Sum of all the Totals(ex: if I give Name as ABC, Dept as First, FY as 2016 - then it should return sum of the required Total values(i.e 100+150=250) for the given FY 2016 only)

请帮助我解决这个问题,谢谢.

Please help me in this requirement, how can I achieve, Thanks.

我在下面尝试过,但是它给出了给定Name的所有结果(例如:如果我给Name作为ABC,那么它将仅返回所有详细信息ABC)

I have tried below, but it is giving all the results for given Name(ex: If I give Name as ABC, then it is returning all the details ABC only)

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} `);
             });

             });

推荐答案

您可以使用

You can use Array.filter() to do that. Filter data based on passed values, and then add Total values of filtered data to get final total.

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" }];

function getTotal(filters) {
  var total = 0;

  const filteredData = data.filter(item => {
    for (var key in filters) {
      if (item[key] != filters[key]) {
        return false;
      }
    }
    return true;
  });

  filteredData.forEach(value => total += Number(value.Total));

  return total;
}

 console.log(getTotal({ "Name": "ABC", "Dept": "First", "FY": "2016" }));

 console.log(getTotal({"Name": "DEF" }));

这篇关于如何根据给定的过滤器返回并获取对象属性的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆