对象中的Sum值表单数组

时间:2016-04-25 17:34:27

标签: javascript arrays node.js 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'
    }]
}

现在我想从价格列获得总和。

我想到了这个

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        
   }

5 个答案:

答案 0 :(得分:2)

这样的事情应该起作用,映射对象,并减少每一个的总和



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>';
&#13;
&#13;
&#13;

结果将是

{
    Fruit: 6, 
    Sport: 130, 
    Kitchen: 15
}

答案 1 :(得分:2)

在您的object stuffData中,您将价格设置为string,如'2' 这意味着你需要将它转换为数字你可以使用parseInt不确定你是否有目的地使用它。无论如何你可以这样做:

var columnSum =[];
//we have here object inside array inside object
// loop for inside the object "stuffData" (i = "Fruit" then "Sport" then "Kitchen")
for (var i in stuffData){
// sum for each column
    var sum = 0;
// loop for inside category ( j = 0 or 1 inside stuffData cuz it is only the position in the array)
    for(var j in stuffData[i]){
    //stuffData[i][j] is the last object. for example "name" :'Ball' , 'price' : '10'
    //stuffData[i][j]['price'] take only the 'price' properties .from the previews example 'price' : '10'
    sum += parseInt(stuffData[i][j]['Price']);
    //sum is the sum of objects category price
    //parseInt is method that convert string to number (cuz if dont use it it will sun the string. for example "1"+"2" = "12")
    }
    columnSum.push(sum);
}
// here columnSum = [6,130,15]

答案 2 :(得分:0)

试试这个

import java.lang.reflect.Field;

public class ConsumeConstants {

    public static void main(String args[]) {

        Field[] fields = MyConstantsPool.class.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {

            Object myConstant = fields[i];
            ConstantEntity typeSafeEnum = (ConstantEntity) myConstant ;
            System.out.println(" The name: "
                    + ((ConstantEntity) typeSafeEnum).constantName());
            System.out.println(" The description: "
                    + ((ConstantEntity) typeSafeEnum).constantDesc());

        }
    }
}

答案 3 :(得分:0)

您可以使用Regex Demo 2对列表求和。

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

如果你使用库Array.prototype.reduce(或类似的东西,例如下划线或Ramda),他们就会得到一个mapValues实用程序,使这更简单:

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

答案 4 :(得分:0)

感谢您的回答。最佳解决方案如下:

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