DiPix
DiPix

Reputation: 6083

Sum value form array in object

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        
   }

Upvotes: 0

Views: 264

Answers (4)

DiPix
DiPix

Reputation: 6083

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

Upvotes: 0

Retsam
Retsam

Reputation: 33389

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

Upvotes: 0

Wasiq Muhammad
Wasiq Muhammad

Reputation: 3118

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

Upvotes: 0

adeneo
adeneo

Reputation: 318172

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
}

Upvotes: 2

Related Questions