Calculating 2 object keys/columns from array of objects Javascript [duplicate]

十年热恋 提交于 2020-07-10 06:42:36

问题


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


回答1:


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)



回答2:


You can use the function Map along with the function reduce as follow

This approach uses an array with the keys in the desired output, that way it's more dynamic.

let foodRevenue = [{"Month": "2011-01",    "week1_Revenue": "100.00",    "week2_Revenue": "300.51",    "Week3_Reenue": "200.09",    "Month1_TotalRevenue": "0"}];
let inArray = ["Month", "week1_Revenue"];
let accumulator = "Month1_TotalRevenue";
let result = foodRevenue.map(revenue => {
  let obj = Object.entries(revenue).reduce((r, [k, v]) => {
    if (inArray.includes(k)) r = Object.assign(r, {[k]: revenue[k]});
    return Object.assign(r, {[accumulator]: +(r[accumulator] || (r[accumulator] = 0)) + +v})
  }, {});
  
  obj[accumulator] = obj[accumulator].toFixed(2);
  return obj;
});

console.log(result);



回答3:


@Yevgen-Gorbunkov's answer is more concise...

    var foodRevenue = [{
        "Month": "2011-01",
        "week1_Revenue": "100.00",
        "week2_Revenue": "300.51",
        "Week3_Revenue": "200.09",
        "Month1_TotalRevenue": "0"
    }];


 let yearlyRevenue = foodRevenue.map((foodRevenue) => {
    let costs = 0

    for(let [key, value] of Object.entries(foodRevenue)) {
      if(key.indexOf('_Revenue') > -1){ // check if the key has _Revenue suffix
        costs += +value // +value change string to number
      }
    }

    console.log(costs);
    
    const transformed = { 
        Month: foodRevenue.Month,
        Month1_TotalRevenue: costs
    }

    return transformed
});

console.log(yearlyRevenue)


来源:https://stackoverflow.com/questions/60639632/calculating-2-object-keys-columns-from-array-of-objects-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!