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 .

4
  • 2
    I tried few types - Please share your attempt. Commented Apr 13, 2018 at 6:54
  • 1
    hint: map + reduce Commented Apr 13, 2018 at 6:55
  • 2
    I think simple reduce will do Commented Apr 13, 2018 at 6:56
  • sure, but with map it's sexier and shorter :p Commented Apr 13, 2018 at 6:57

2 Answers 2

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);

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

3 Comments

.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 Thanks. Ya forEach is better option. Let me add it in my answer too.
Still, .map implementation is wrong
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);

2 Comments

Should we not close this post as dupe instead?
Also, .map implementation is just wrong

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.