Guest User

Untitled

a guest
Jan 21st, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. groupBy(x=>x.Cart)
  2. .aggregate(g => {
  3. var products = g.values.reduce((agg, item) => {
  4. for (var key in item.Products) {
  5. var p = item.Products[key];
  6. var existing = agg[key];
  7. if (!existing) {
  8. agg[key] = { Quantity: p.Quantity, Price: p.Price };
  9. }
  10. else {
  11. existing.Quantity += p.Quantity;
  12. if(p.Price > 0)
  13. existing.Price = Math.min(p.Price, existing.Price);
  14. }
  15. }
  16. return agg;
  17. }, {});
  18.  
  19. for (var key in products) {
  20. if(products[key].Quantity == 0)
  21. delete products[key];
  22. }
  23.  
  24. return {
  25. Cart: g.key,
  26. Products: products
  27. };
  28. })
Add Comment
Please, Sign In to add comment