简体   繁体   English

JavaScript:将对象嵌套数组中的数字相加

[英]JavaScript: Add up numbers in nested array of objects

I'm trying to get the average price, multiplie price and add up qty of nested array of objects but i dont know how to get te right result can somebody help.我正在尝试获得平均价格,将价格相乘并将嵌套对象数组的数量相加,但我不知道如何获得正确的结果,有人可以帮忙。 Thanks in advance提前致谢

Array type:数组类型:

[
CreatAsset_kcnuvkr7: (3) [{…}, {…}, {…}]
CreatAsset_kcop95ya: (2) [{…}, {…}]
CreatAsset_kcoprqc4: (3) [{…}, {…}, {…}]
CreatAsset_kcqjmhyh: (5) [{…}, {…}, {…}, {…}, {…}]
]

Object data: Object 数据:

[
  {
    latestPrice: { stringValue: 207.88 },
    purchaseDate: { stringValue: "1594829893159" },
    purchaseQty: { stringValue: "50" },
    waterMark: { stringValue: "CreatAsset_kcnuvkr7" }
  },
  {
    latestPrice: { stringValue: 9.88 },
    purchaseDate: { stringValue: "1593868712336" },
    purchaseQty: { stringValue: "30.00" },
    waterMark: { stringValue: "CreatAsset_kcnuvkr7" }
  },
  {
    latestPrice: { stringValue: 98.8 },
    purchaseDate: { stringValue: "1594829859268" },
    purchaseQty: { stringValue: "100" },
    waterMark: { stringValue: "CreatAsset_kcnuvkr7" }
  }
];

Result i want:我想要的结果:

(totalPrice = latestPrice * purchaseQty ), (avgPrice = (latestPrice index[0] + latestPrice index[1] + latestPrice index[2] / 3) (totalPrice = latestPrice * purchaseQty ), (avgPrice = (latestPrice index[0] + latestPrice index[1] + latestPrice index[2] / 3)

{
  avgPrice: {
    stringValue: 105.52
  }
  totalPurchaseQty: {
    stringValue: "180"
  }
  totalPrice1: {
    stringValue: "10394.00"
  }
  totalPrice2: {
    stringValue: "296.40"
  }
  totalPrice3: {
    stringValue: "9880.00"
  }
  waterMark: {
    stringValue: "CreatAsset_kcnuvkr7"
  }
}

My Code:我的代码:

(result is the Array type of above)

let latestPrice = [];
for (let i in result) {
  if (result.hasOwnProperty(i)) {
    result[i].map((res) => {
      latestPrice.push({
        latestPrice: res.latestPrice.stringValue,
        waterMark: res.waterMark.stringValue,
      });
    });
  }
}
Object.entries(data).forEach(([watermark, array]) => {
  data[watermark] = array.reduce((result, object, i) => {
    const { latestPrice, purchaseQty } = Object.fromEntries(Object.entries(object)
      .map(([key, { stringValue }]) => ([key, +stringValue])));
    result['totalPrice' + (i + 1)] = latestPrice * purchaseQty;
    result.avgPrice += latestPrice;
    result.totalPurchaseQty += purchaseQty;
    i == array.length - 1 && (result.avgPrice /= array.length);
    return result;
  }, { avgPrice: 0, totalPurchaseQty: 0, watermark });
});

Live example:现场示例:

 const data = { CreatAsset_kcnuvkr7: [ { latestPrice: { stringValue: 207.88 }, purchaseDate: { stringValue: '1594829893159' }, purchaseQty: { stringValue: '50' }, waterMark: { stringValue: 'CreatAsset_kcnuvkr7' } }, { latestPrice: { stringValue: 9.88 }, purchaseDate: { stringValue: "1593868712336" }, purchaseQty: { stringValue: "30.00" }, waterMark: { stringValue: "CreatAsset_kcnuvkr7" } }, { latestPrice: { stringValue: 98.80 }, purchaseDate: { stringValue: "1594829859268" }, purchaseQty: { stringValue: "100" }, waterMark: { stringValue: "CreatAsset_kcnuvkr7" } } ] } Object.entries(data).forEach(([watermark, array]) => { data[watermark] = array.reduce((result, object, i) => { const { latestPrice, purchaseQty } = Object.fromEntries(Object.entries(object).map(([key, { stringValue }]) => ([key, +stringValue]))); result['totalPrice' + (i + 1)] = latestPrice * purchaseQty; result.avgPrice += latestPrice; result.totalPurchaseQty += purchaseQty; i == array.length - 1 && (result.avgPrice /= array.length); return result; }, { avgPrice: 0, totalPurchaseQty: 0, watermark }); }); console.log(data);

Perhaps you are after something like this:也许你在追求这样的事情:

 const data = [ { latestPrice: { stringValue: 207.88 }, purchaseDate: { stringValue: "1594829893159" }, purchaseQty: { stringValue: "50" }, waterMark: { stringValue: "CreatAsset_kcnuvkr7" } }, { latestPrice: { stringValue: 9.88 }, purchaseDate: { stringValue: "1593868712336" }, purchaseQty: { stringValue: "30.00" }, waterMark: { stringValue: "CreatAsset_kcnuvkr7" } }, { latestPrice: { stringValue: 98.8 }, purchaseDate: { stringValue: "1594829859268" }, purchaseQty: { stringValue: "100" }, waterMark: { stringValue: "CreatAsset_kcnuvkr7" } } ]; const average = arr => arr.reduce((p, c) => p + c, 0) / arr.length; console.log("Average Price", average(data.map(d => d.latestPrice.stringValue))); const total = data.map(d => (d.latestPrice.stringValue * d.purchaseQty.stringValue).toFixed(2) ); console.log("Total prices:", total); const totalPurchaseQty = data.map(d => d.purchaseQty.stringValue).reduce((a, b) => Number(a) + Number(b), 0); console.log("Total Purchse quantity:", totalPurchaseQty);

It's just fun, enjoyable functional programming.这只是有趣、令人愉快的函数式编程。 You can use .map() to transform your objects in the values you want to work with and apply your formulas from there.您可以使用.map()将对象转换为您想要使用的值并从那里应用您的公式。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM