如何找到数组中相似对象的平均值并将其输出到一个对象中?

时间:2018-10-30 14:23:04

标签: javascript node.js

我有一个具有这种结构的对象数组:

    [
      costBreakdown: {
        flexMeetAncCost: "1,274,051",
        flexOneTimeCosts: "0",
        flexSeatCharges: "2,403,869",
        tradFacilMgmtCost: "134,437",
        tradOneTimeTotalCost: "1,462,049",
        tradRentCost: "1,572,738",
      },

      costBreakdown: {
        flexMeetAncCost: "1,279,524",
        flexOneTimeCosts: "0",
        flexSeatCharges: "2,414,197",
        tradFacilMgmtCost: "135,025",
        tradOneTimeTotalCost: "1,467,029",
        tradRentCost: "1,579,576",
      },
    ]

并且我试图遍历所有这些对象,并在解析字符串并将其设为整数后找到每个键的平均值,然后输出一个聚合对象。 / p>

const { mean } = require('lodash')

  const result = allLambdas.reduce((acc, currValue, index, array) => {
    const aggregateCostBreakdown = (key) => (
      Math.round(mean(array.map((obj) => parseFloat(obj.costBreakdown[key].replace(/,/g, ''))))).toLocaleString('en')
    )

    const avgCostBreakdown = {
      flexMeetAncCost: aggregateCostBreakdown('flexMeetAncCost'),
      flexOneTimeCosts: aggregateCostBreakdown('flexOneTimeCosts'),
      flexSeatCharges: aggregateCostBreakdown('flexSeatCharges'),
      tradFacilMgmtCost: aggregateCostBreakdown('tradFacilMgmtCost'),
      tradOneTimeTotalCost: aggregateCostBreakdown('tradOneTimeTotalCost'),
      tradRentCost: aggregateCostBreakdown('tradRentCost')
    }

    acc.costBreakdown = avgCostBreakdown

    return acc
  }, { 
      costBreakdown: {},
     }
    )

尽管这似乎可行,但我认为我永远都不应在array函数中引用reduce参数,因为这似乎没有效果。

如何获取这些对象数组的平均值并将它们输出到一个对象中?

2 个答案:

答案 0 :(得分:1)

您可以尝试类似的操作,在此过程中,您首先将值求和并保持计数,然后找到每个道具的均值。希望这会有所帮助。

const data = [{
    costBreakdown: {
        flexMeetAncCost: "1,274,051",
        flexOneTimeCosts: "0",
        flexSeatCharges: "2,403,869",
        tradFacilMgmtCost: "134,437",
        tradOneTimeTotalCost: "1,462,049",
        tradRentCost: "1,572,738",
    } },{
    costBreakdown: {
        flexMeetAncCost: "1,279,524",
        flexOneTimeCosts: "0",
        flexSeatCharges: "2,414,197",
        tradFacilMgmtCost: "135,025",
        tradOneTimeTotalCost: "1,467,029",
        tradRentCost: "1,579,576",
    },
    }
];

const findMeans = (arr) => {

    const summed = arr.reduce((acc, { costBreakdown }) => {

        Object.keys(costBreakdown).forEach((key) => {

            const n = parseFloat(costBreakdown[key].replace(',', '.'));

            acc.costBreakdown[key] = acc.costBreakdown[key] ? { value: acc.costBreakdown[key].value + n, count: acc.costBreakdown[key].count + 1 } : { value: n, count: 1 }
        });

        return acc;

    }, { costBreakdown: {} });

    return Object.keys(summed.costBreakdown).reduce((acc, val) => {

        acc.costBreakdown[val] = summed.costBreakdown[val].value / summed.costBreakdown[val].count;

        return acc;

    }, { costBreakdown: {} });
};

console.log(findMeans(data));

答案 1 :(得分:0)

如果我理解正确,则需要将数组简化为与数组内元素具有相同结构的对象,但结果数组中属性的值应为元素所有共同响应的平均值,对吧?

可能是这样的事情应该起作用:

const convertToNumber = parseFloat; // make your own implementation
const allLambdasLength = allLambdas.length;

const result = allLambdas.reduce((acc, currValue, index) => {
    if(index < allLambdasLength - 1) {
        return {
            flexMeetAncCost: acc.flexMeetAncCost + convertToNumber(currValue.flexMeetAncCost),
            // do this for all keys in the object
            ...
        };
    } else {
      return {
          costBreakdown: {
            flexMeetAncCost: acc.flexMeetAncCost / allLambdasLength,
            // do this for all keys in the object
            ...
          }
      };
    }
}, {})