0

I'm trying to group the following array of objects by the key amount_per_btc where the values are the same, and then sum the values of the key btc_qty. Below is the ungrouped sample.

[ { amount_per_btc: 2600000, btc_qty: 0.001 },
 { amount_per_btc: 2600000, btc_qty: 0.001 },
 { amount_per_btc: 2600000, btc_qty: 0.001 },
 { amount_per_btc: 150000000, btc_qty: 0.001 },
 { amount_per_btc: 150000000, btc_qty: 0.01 },
 { amount_per_btc: 150000000, btc_qty: 0.001 } ]

My result should look like this:

[ { amount_per_btc: 150000000, btc_qty: 0.012 },
  { amount_per_btc: 2600000, btc_qty: 0.003 }]

I'm using nodejs so a pure javascript or lodash solution will be appreciated.

Chibuzo
  • 6,112
  • 3
  • 29
  • 51
  • for each object compare the 'amount_per_btc' value with the rest of the array's objects, if they're equal: sum their btc_qty values and then remove them. Continue doing so until you've removed all the objects from the array. – Ofir Baruch Nov 02 '17 at 07:01
  • 1
    https://jsfiddle.net/qf7roey4/ here is the solution – Ezzat Nov 02 '17 at 07:53

0 Answers0