0

I've an object:

var stuffData = {
    'Fruit': [{
        'Name': 'Apple',
        'Price': '2'
    }, {
        'Name': 'Kiwi',
        'Price': '4'
    }],
    'Sport': [{
        'Name': 'Ball',
        'Price': '10'
    }, {
        'Name': 'Bike',
        'Price': '120'
    }],
    'Kitchen': [{
        'Name': 'Knife',
        'Price': '8'
    }, {
        'Name': 'Fork',
        'Price': '7'
    }]
}

Now i want to get sum from Price Column.

I thought about this

for (var key in stuffData)
   {
     // and for each key i have to add new array with sum of price or what?
     // But how will I display this sum then?
     // I haven't any idea how can I deal with this        
   }
3
  • You want the sum of all the Price values, regardless of category ? Commented Apr 25, 2016 at 17:36
  • Each category has own sum of price Commented Apr 25, 2016 at 17:38
  • Maybe you should post the result you want then, to show us the format etc. Commented Apr 25, 2016 at 17:40

4 Answers 4

2

Something like this should work, mapping the objects, and reducing to sum each one

var stuffData = {
    'Fruit': [{
        'Name': 'Apple',
        'Price': '2'
    }, {
        'Name': 'Kiwi',
        'Price': '4'
    }],
    'Sport': [{
        'Name': 'Ball',
        'Price': '10'
    }, {
        'Name': 'Bike',
        'Price': '120'
    }],
    'Kitchen': [{
        'Name': 'Knife',
        'Price': '8'
    }, {
        'Name': 'Fork',
        'Price': '7'
    }]
}

var o = {};

Object.keys(stuffData).forEach(function(key) {
    o[key] = stuffData[key].map(function(item) {
    	return parseInt(item.Price, 10);
    }).reduce(function(a,b) {
    	return a + b;
    });
});

document.body.innerHTML = '<pre>' + JSON.stringify(o, 0, 4) + '</pre>';

The result would be

{
    Fruit: 6, 
    Sport: 130, 
    Kitchen: 15
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try like this

for (var key in stuffData) {
var totalSum =0;
    if(stuffData[key] = 'Fruit'{
        for(var i=0; i< stuffData[key].length ; i++){
            totalSum+=stuffData[key].price;
        }
    }

    if(stuffData[key] = 'Sport'{
        for(var i=0; i< stuffData[key].length ; i++){
            totalSum+=stuffData[key].price;
        }
    }

    if(stuffData[key] = 'Kitchen'{
        for(var i=0; i< stuffData[key].length ; i++){
            totalSum+=stuffData[key].price;
        }

    }
    console.log("TOTAL SUM",totalSum);
}

Comments

0

You can use Array.prototype.reduce to sum over a list.

var categorySums = {};
for(category in stuffData) {
    var items = stuffData[category]; 
    categorySums[category] = items.reduce(function (sum, item) { 
         return sum + parseInt(item.Price);
    }, 0);
}

If you use the library lodash (or something similar, e.g. underscore or Ramda), they've got a mapValues utility that makes this simpler:

var categorySums = _.mapValues(stuffData, function(items) {
    return _.sum(items.map(function (item) {
        return parseInt(item.Price);
    }));
});

Comments

0

Thank you for every answer. The best solution is below:

   var sum = {};
    for (var k in stuffData)
    {
       sum[k] = 0;
       stuffData[k].forEach(function (e)
       {
          sum[k] += parseInt(e.price);
       });
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.