4

I'm using the following to process rows from shopping cart. I'm wanting to group the rows and sum the values in the grouping. I'm not able to get the sum to work at all and I was hoping to get some assistance please.

thanks in advance

-- data

var cart: [
      {
        id: 2,
        title: 'Batteries',
        description: 'AAA size batteries',
        price: 10.99
      },
      {
        id: 1,
        title: 'Beacons',
        description: 'Bluetooth long life beacons',
        price: 30.00
      },
      {
        id: 1,
        title: 'Beacons',
        description: 'Bluetooth long life beacons',
        price: 30.00
      }
    ]

-- code

 const groupedResult = _(cart)
    .groupBy('price')
    .map(function(items, price, title) {
      return {
        title: _.map(items, 'title'),
        description: _.map(items, 'description'),
         price: _.sum(price, 'price'),
      };
    }).value()

-- current output

    {title: Array(2), description: Array(2), price: "30"}
    {title: Array(1), description: Array(1), price: "10.99"}

-- expected output

   {title: 'Beacons', description: 'Bluetooth long life beacons', price: '30.00', total: '60.00', quantity: 2}
   {title: 'Batteries', description: 'AAA size batteries', price: '10.99', total: '10.99', quantity: 1}
5
  • 1
    how do you want to group it? Commented Mar 5, 2018 at 4:59
  • do you want to group by id and sum all prices? Commented Mar 5, 2018 at 5:00
  • yes I do. I tried various ways to do this but I kept getting o or undefined for the prices. Commented Mar 5, 2018 at 5:11
  • guijob I realise this, I thought that I might be able to map the prices and then sum inside the map like : price: ._map(.sum(price, 'price')) Commented Mar 5, 2018 at 5:28
  • include your expected output Commented Mar 5, 2018 at 5:29

2 Answers 2

3

groupBy returns an object instead of an array, but you can get this values using Object.values once you unwrap your result using Array.from(). So, assuming title, description and price with same values for same id, this should work:

 const groupedResult = Object.values(Array.from(_(cart)
 .groupBy('id')))
 .map(x => ({
   title: x[0].title,
   description: x[0].description,
   price: x[0].price,
   quantity: x.length,
   total: _.sumBy(x, x => x.price)
 }));

Note: sumBy is only available in lodash 4.0.0 and above.

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

Comments

2

You can reduce the array into a Map, and sum the prices if an id already exists on the Map. Then spread the Map.values() back to an array:

const cart = [{"id":2,"title":"Batteries","description":"AAA size batteries","price":10.99},{"id":1,"title":"Beacons","description":"Bluetooth long life beacons","price":30},{"id":1,"title":"Beacons","description":"Bluetooth long life beacons","price":30}];
    
const result = [...cart.reduce((r, o) => {
  // add a new item to the map, and set price and quantity to 0
  r.has(o.id) || r.set(o.id, Object.assign({}, o, { price: 0, quantity: 0 }));
  
  // get current item;
  const item = r.get(o.id);
  
  // add current price to item
  item.price += o.price;
  
  // increment quantity
  item.quantity++;

  return r;
}, new Map()).values()]; // spread the values iterator back to array

console.log(result);

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.