如何汇总数组中对象的属性并将其存储到父对象?

时间:2019-09-08 19:37:36

标签: javascript arrays json vue.js sum

我有一个对象数组,例如:

var obj = {
   "ABC" : {
      "name" : "ABC",
      "budget" : 0,
      "expense" : 0,
      "ledgers" : [{
        "Actual1920": 10,
       "Budget1920": 20,
      },
      {
       "Actual1920": 10,
       "Budget1920": 10,
      }]
  },
  "PQR" : {
    "name" : "PQR",
    "budget" : 0,
    "expense" : 0,
    "ledgers" : [{
      "Actual1920": 10,
      "Budget1920": 20,
    }]
  }
}

我想对ABC对象和Ledgers数组中的Actual1920求和,并将其存储在ABC的预算中,并作为支出

预期结果

var obj = {
   "ABC" : {
      "name" : "ABC",
      "budget" : 30,
      "expense" : 20,
      "ledgers" : [{
        "Actual1920": 10,
       "Budget1920": 20,
      },
      {
       "Actual1920": 10,
       "Budget1920": 10,
      }]
  },
  "PQR" : {
    "name" : "PQR",
    "budget" : 20,
    "expense" : 10,
    "ledgers" : [{
      "Actual1920": 10,
      "Budget1920": 20,
    }]
  }
}

将Actual1920的总和存储为费用,将Budget1920的总和存储为预算。

2 个答案:

答案 0 :(得分:1)

您可以使用ledgersforEach()函数获得所需字段新数组名称.map()的总和。

var arr =  [  {
    "name": "Salary",
    "budget": 0,
    "expense": 0,
    "remaing": 0,
    "ledgers": [
    {
    "Actual1920": 9009006,
    "Budget1920": 46861141.9709555,
    "CostOwner": "Lakshmi Mohan",
    "LTRevExp": "Expense-EB",
    "LedgerBudget": "Salary",
    "LedgereType": "Salary-Teaching",
    "RemainingAmount": 37852135.9709555,
    "leadgerLevel": "EBITDA",
    "quaterFour": 11715285.492738875,
    "quaterOne": 11715285.492738875,
    "quaterThree": 11715285.492738875,
    "quaterTwo": 11715285.492738875
    },
    {
    "Actual1920": 7765368,
    "Budget1920": 33788679.599044524,
    "CostOwner": "Lakshmi Mohan",
    "LTRevExp": "Expense-EB",
    "LedgerBudget": "Salary",
    "LedgereType": "Salary-Non Teaching",
    "RemainingAmount": 26023311.599044524,
    "leadgerLevel": "EBITDA",
    "quaterFour": 8447169.899761131,
    "quaterOne": 8447169.899761131,
    "quaterThree": 8447169.899761131,
    "quaterTwo": 8447169.899761131
    },
    {
    "Actual1920": 0,
    "Budget1920": 0,
    "CostOwner": "Lakshmi Mohan",
    "LTRevExp": "Expense-EB",
    "LedgerBudget": "Salary",
    "LedgereType": "Salary-Contract & Professional",
    "RemainingAmount": 0,
    "leadgerLevel": "EBITDA",
    "quaterFour": 0,
    "quaterOne": 0,
    "quaterThree": 0,
    "quaterTwo": 0
    }
    ]
    },
    {
    "name": "Scholarship & Discounts",
    "budget": 0,
    "expense": 0,
    "remaing": 0,
    "ledgers": [
    {
    "Actual1920": 1460000,
    "Budget1920": 15977747.5,
    "CostOwner": "Vineeta S",
    "LTRevExp": "Expense-EB",
    "LedgerBudget": "Scholarship & Discounts",
    "LedgereType": "Scholarship & Discount",
    "RemainingAmount": 14517747.5,
    "leadgerLevel": "EBITDA",
    "quaterFour": 0,
    "quaterOne": 3723000,
    "quaterThree": 12254747.5,
    "quaterTwo": 0
    }
    ]
    }
   ]

var ledger = {"salary" : 0 , "Expense" : 0 , "remaining" : 0};
arr.forEach(function(singleObject){
    singleObject.ledgers.forEach(function(singleArrObject){
        Object.keys(singleArrObject).map((o)=>{
            if(o == "Actual1920"){

                ledger.salary = ledger.salary + singleArrObject[o]; 
            }
            else if(o == "Budget1920"){
                ledger.Expense = ledger.Expense + singleArrObject[o];
            }
            else if(o == "RemainingAmount"){
                ledger.remaining = ledger.remaining + singleArrObject[o];
            }
        })
    })
});
console.log(ledger);

答案 1 :(得分:0)

    arr.forEach(function(singleObject){
       console.log(singleObject);
       singleObject.ledgers.forEach(function(singleArrObject){
        Object.keys(singleArrObject).map((o)=>{
            if(o == "Actual1920"){

                singleObject.expense = singleObject.expense + singleArrObject[o]; 
            }
            else if(o == "Budget1920"){
                singleObject.budget = singleObject.budget + singleArrObject[o];
            }
            else if(o == "RemainingAmount"){
                singleObject.remaining = singleObject.remaining + singleArrObject[o];
            }
        })
    })
});