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,
});
});
}
}
avgPriceto be the sum of everylatestPricedivied by the sum of everypurchaseQty, but in your example you actually computed it as the sum of everylatestPricedivided by the length of the array.