Advertisement
Guest User

Untitled

a guest
Sep 11th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. getInfo() {
  2.             let info = {};
  3.  
  4.             info.totalPrice = this.showCost();
  5.  
  6.             info.products = [];
  7.  
  8.             let names = {};
  9.  
  10.             for (let i = 0; i < this.products.length; i += 1) {
  11.                 if (!names[this.products[i].name]) {
  12.                     names[this.products[i].name] = {};
  13.                     names[this.products[i].name].price = 0;
  14.                     names[this.products[i].name].quantity = 0;
  15.                 }
  16.  
  17.                 names[this.products[i].name].price += this.products[i].price;
  18.                 names[this.products[i].name].quantity += 1;
  19.             }
  20.  
  21.             let props = Object.keys(names);
  22.  
  23.             for (let i = 0; i < props.length; i += 1) {
  24.                 info.products.push({
  25.                     name: props[i],
  26.                     totalPrice: names[props[i]].price,
  27.                     quantity: names[props[i]].quantity
  28.                 });
  29.             }
  30.  
  31.             return info;
  32.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement