0

i am having an array of Product with fields name ID,Brand,Price,QtySold,Value where value=Price*qtySold and at the end i need to show Number of Items,Total quantity sold and total sales value

 @Component
({
 selector: 'my-app',
 templateUrl: './app.component.html',
 styleUrls: [ './app.component.css' ]
 }`
export class AppComponent{
allProduct:Product[]=[
{Id:'P104', Brand:'Pepsi',Price:4,qtySold:22},
{Id:'C124', Brand:'Coke',Price:4,qtySold:26},
{Id:'M155', Brand:'Maggie',Price:6,qtySold:10},
{Id:'DM241', Brand:'Cadburys',Price:10,qtySold:15},
{Id:'5S118', Brand:'5 Star',Price:8,qtySold:8},
];

Need to display the Number of Products,Sum of Quantity sold and sum of Sales Value

1
  • need to do this using *ngOninit.. Feb 14 '19 at 18:02
3

You will need something like below within your ngOninit

let products = [
  {
    "Id": "P104",
    "Brand": "Pepsi",
    "Price": 4,
    "qtySold": 22
  },
  {
    "Id": "C124",
    "Brand": "Coke",
    "Price": 4,
    "qtySold": 26
  },
  {
    "Id": "M155",
    "Brand": "Maggie",
    "Price": 6,
    "qtySold": 10
  },
  {
    "Id": "DM241",
    "Brand": "Cadburys",
    "Price": 10,
    "qtySold": 15
  },
  {
    "Id": "5S118",
    "Brand": "5 Star",
    "Price": 8,
    "qtySold": 8
  }
];

let productsCount = products.length;
let qtySold = products.reduce((a, b) => +a + +b.qtySold, 0);
let sales = products.reduce((a, b) => +a + +b.Price, 0);

console.log(productsCount);
console.log(qtySold);
console.log(sales);

STACKBLITZ DEMO

8
  • need to write this in component.ts right?? i am writing in it but it is showing Unexpected Token. i am writing it just below my codes given above. Feb 14 '19 at 18:22
  • you might not need to use let Feb 14 '19 at 18:26
  • can you just make it simple for me by writing the code including *ngOninit beacuse m having issue in writing it..i am facing the same problem since today morning..can you pelase help me with it.. Feb 14 '19 at 18:30
  • hey can you give me an another method rather than reduce to sum up these elements Feb 16 '19 at 5:55
  • what is the simple way to add them by running a for loop..by not using this terms like reduce,sumBy forEach..just the basic method Feb 16 '19 at 6:30
1

Either simple reduce like @Sajeetharan posted or using some util library like lodash:

this.numberOfProducts = allProduct.length;
this.sumQtySold = _.sumBy(allProduct, product => product.qtySold);
this.sales = _.sumBy(allProduct, product => product.qtySold * product.price);
0
0

Another way to accomplish this:

const beverageSales = [
  {
    "Id": "P104",
    "Brand": "Pepsi",
    "Price": 4,
    "qtySold": 22
  },
  {
    "Id": "C124",
    "Brand": "Coke",
    "Price": 4,
    "qtySold": 26
  },
  {
    "Id": "M155",
    "Brand": "Maggie",
    "Price": 6,
    "qtySold": 10
  },
  {
    "Id": "DM241",
    "Brand": "Cadburys",
    "Price": 10,
    "qtySold": 15
  },
  {
    "Id": "5S118",
    "Brand": "5 Star",
    "Price": 8,
    "qtySold": 8
  }
];

let itemsSold = 0;
let quantitySold = 0;
let netSales = 0;

beverageSales.forEach(sale => {
  itemsSold += 1;
  quantitySold += sale.qtySold;
  netSales += sale.Price * sale.qtySold;
});

console.log('items sold', itemsSold);
console.log('quantity sold', quantitySold);
console.log('net sales', netSales);

0
0
productCount : number=0;
quantitySold : number=0;
sales : number=0;
sold : number=0;
ngOnInit(){
for(let temp of this.allProduct){
this.productCount += 1;
this.quantitySold += temp.qtySold;
this.sales += temp.Price * temp.qtySold;
if(temp.qtySold>0){
this.sold += 1;
}}}

The above code worked for me.It has been used to sum up the values just by using Loops and no other terminologies.

3
  • 1
    Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review Feb 16 '19 at 9:15
  • No.! this the Soluntion i have posted for my Question as i got the solution. Feb 16 '19 at 9:30
  • Then please add an explanation, just dumping code doesn't make a good answer. Feb 16 '19 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.