2

i have this array

const input = [
  { StockKeepingUnit: 'SKU-001', Quantity: 3 },
  { StockKeepingUnit: 'SU-002', Quantity: 5 },
  { StockKeepingUnit: 'SU-002', Quantity: 7 },
  { StockKeepingUnit: 'SKU-d7', Quantity: 2 },
  { StockKeepingUnit: 'SKU-d7', Quantity: 4 },
  { StockKeepingUnit: 'sku-899', Quantity: 1 },
  { StockKeepingUnit: 'SU-002', Quantity: 1 },
  { StockKeepingUnit: 'SU-002', Quantity: 2 },
];

but I would like for it to be like this

const input = [
  { StockKeepingUnit: 'SKU-001', Quantity: 3 },
  { StockKeepingUnit: 'SU-002', Quantity: 15 },
  { StockKeepingUnit: 'SKU-d7', Quantity: 6 },
  { StockKeepingUnit: 'sku-899', Quantity: 1 },
];

So reduce the array but add the quantities together, please, this is driving me crazy. Thank you!

1 Answer 1

2

I think that the best approach would be first doing all the sums, and the formatting as you like

Object.entries(
   input
      .reduce(
         (prev, current) => ({...prev, [current.StockKeepingUnit]: (prev[current.StockKeepingUnit] || 0) + current.Quantity}),
         {}
      )
).map(([StockKeepingUnit, Quantity]) => ({StockKeepingUnit, Quantity}))

For your second question, it would be very similar to the first answer, but instead of adding the values, you should add them to lists, and then reduce the items again in order to calculate the average

Object.entries(
   input
      .reduce(
         (prev, current) => ({...prev, [current.StockKeepingUnit]: [...(prev[current.StockKeepingUnit] || []), current.Quantity]}),
         {}
      )
)
    .map(([StockKeepingUnit, Quantity]) => ({StockKeepingUnit, Average: Quantity.reduce((a, b) => a + b, 0) / Quantity.length}))
3
  • Amazing thank you. Now say I have this resulted array [{"StockKeepingUnit":"SKU-001","Quantity":3},{"StockKeepingUnit":"SU-002","Quantity":15},{"StockKeepingUnit":"SKU-d7","Quantity":6},{"StockKeepingUnit":"sku-899","Quantity":1}] and i wanted to find the average of quanties. so the array above is the toatl amount and the array below is how many times ordered { 'SKU-001': 1, 'SU-002': 4, 'SKU-d7': 2, 'sku-899': 1 } using the 2 arrays I want to divide total amount by times ordered and then round it up?
    – knay
    Commented Sep 26, 2022 at 18:48
  • 1
    updated the answer to also calculate the average
    – mnunezdm
    Commented Sep 26, 2022 at 19:50
  • Please can you show me how I would do the same for this array { 'SKU-001': { StockKeepingUnit: 'SKU-001', Description: true, '2022-09-07': 3 }, 'SU-002': { StockKeepingUnit: 'SU-002', Description: false, '2022-09-07': 5, '2022-09-20': 7, 'SKU-d7': { StockKeepingUnit: 'SKU-d7', Description: false, '2022-09-20': 2, '2022-09-23': 4 }, } so keeping all values the same, but adding an average field for each which will be total order / times ordered. The input could already have an empty average field if that makes it easier
    – knay
    Commented Sep 27, 2022 at 15:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .