1

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,
      });
    });
  }
}
3
  • Please reformat your code as valid JSON or JavaScript. Commented Jul 22, 2020 at 15:53
  • You say you want avgPrice to be the sum of every latestPrice divied by the sum of every purchaseQty, but in your example you actually computed it as the sum of every latestPrice divided by the length of the array. Commented Jul 22, 2020 at 16:10
  • @GirkovArpa yes you are right sorry i'm going to edit Commented Jul 22, 2020 at 16:19

2 Answers 2

2
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);

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.