Q:

Calculating 2 object keys/columns from array of objects Javascript

So I was working on the some column merging and filtering,but I am having trouble calculating 2 object keys (columns) from an array of objects and then mapping that result to an object key to a new array
Example:
var foodRevenue = [{
    "Month": "2011-01",
    "week1_Revenue": "100.00",
    "week2_Revenue": "300.51",
    "Week3_Reenue": "200.09",
    "Month1_TotalRevenue": "0"
}];

I want to calculate the sum of  week1, week2, and week3 and the result to map on Month1_TotalRevenue. Then creating a new array that would filter Month and Month1_TotalRevenue to that array. Like so:
{"Month": "2011-01", "Month1_TotalRevenue": "600.60"}

Any suggestions would be appreciated

A:

You may destructure your object properties and sum up desired ones, using Array.prototype.reduce():

const  foodRevenue = {
      "Month": "2011-01",
      "week1_Revenue": "100.00",
      "week2_Revenue": "300.51",
      "Week3_Reenue": "200.09",
      "Month1_TotalRevenue": "0"
    },
    {Month, Month1_TotalRevenue, week1_Revenue,...revenue} = foodRevenue,
    result = {
      Month,
      week1_Revenue,
      Month1_TotalRevenue: 
        Object
          .values({...revenue,week1_Revenue})
          .reduce((s,r) => s+=+r, +Month1_TotalRevenue)
          .toFixed(2)
    }
    
console.log(result)

