简体   繁体   中英

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:

[
  {
    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)

{
  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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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