0
[
   {
      "ProductId": 177,
      "Quantity": 3,
      "Price": 1,
      "OriginalPrice": 1,
      "OriginalPricetotal": 3,
      "SoldPrice": 1,
      "ProductName": "Hand Spinners"
   },
   {
      "ProductId": 30207,
      "Quantity": 2,
      "Price": 525,
      "OriginalPrice": 525,
      "OriginalPricetotal": 1050,
      "SoldPrice": 525,
      "ProductName": "Clay Home"
   }
]

I want to calculate sum of all 'OriginalPricetotal' in an array using javascript / Jquery. I tried few types . But that is not working .

Akhila Prakash
  • 481
  • 4
  • 17

2 Answers2

3

Please see below snippet which will return you the answer you want

var obj = [
   {
      "ProductId": 177,
      "Quantity": 3,
      "Price": 1,
      "OriginalPrice": 1,
      "OriginalPricetotal": 3,
      "SoldPrice": 1,
      "ProductName": "Hand Spinners"
   },
   {
      "ProductId": 30207,
      "Quantity": 2,
      "Price": 525,
      "OriginalPrice": 525,
      "OriginalPricetotal": 1050,
      "SoldPrice": 525,
      "ProductName": "Clay Home"
   }
];

var ans = 0;
// Better option
obj.forEach(function(value){
 ans = ans + value.OriginalPricetotal
});
console.log(ans);

ans = 0;
//obj.map(o => ans = ans + o.OriginalPricetotal); //ES6
obj.map(function(value){
 ans = ans + value.OriginalPricetotal
});
console.log(ans);
  • 1
    `.map` without a return statement and assignment of final value is just a bad implementation. Please use `.forEach` instead. Also, though this is opinionated comment, answering a question without effort promotes spoon feeding – Rajesh Apr 13 '18 at 07:06
  • @Rajesh Thanks. Ya forEach is better option. Let me add it in my answer too. – Yatendrasinh Joddha Apr 13 '18 at 07:09
  • Still, `.map` implementation is wrong – Rajesh Apr 13 '18 at 07:12
0

You just need to loop through the array and get the value of OriginalPricetotal to add and get the sum. Since, the value is already a number and not a string, you do not need parseInt() to parse the values before addition:

USING forEach()

var arr = [
   {
      "ProductId": 177,
      "Quantity": 3,
      "Price": 1,
      "OriginalPrice": 1,
      "OriginalPricetotal": 3,
      "SoldPrice": 1,
      "ProductName": "Hand Spinners"
   },
   {
      "ProductId": 30207,
      "Quantity": 2,
      "Price": 525,
      "OriginalPrice": 525,
      "OriginalPricetotal": 1050,
      "SoldPrice": 525,
      "ProductName": "Clay Home"
   }
];

var sum = 0;
arr.forEach(function(obj){
  sum += obj.OriginalPricetotal;
});

console.log(sum);

USING map()

var arr = [
   {
      "ProductId": 177,
      "Quantity": 3,
      "Price": 1,
      "OriginalPrice": 1,
      "OriginalPricetotal": 3,
      "SoldPrice": 1,
      "ProductName": "Hand Spinners"
   },
   {
      "ProductId": 30207,
      "Quantity": 2,
      "Price": 525,
      "OriginalPrice": 525,
      "OriginalPricetotal": 1050,
      "SoldPrice": 525,
      "ProductName": "Clay Home"
   }
];

var sum = 0;
arr.map((obj)=>{sum += obj.OriginalPricetotal});

console.log(sum);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62