如何求和对象的属性值?

时间:2016-07-23 09:24:01

标签: javascript jquery arrays object

我想对PieData的属性值求和。我的预期产量是 25515512+916952499 = 942468011

var PieData = [
    {
        value: 25515512,
        color: "#00a65a",
        highlight: "#00a65a",
        label: "Received Fund"     
    },
    {
        value: 916952499,
        color: "#f56954",
        highlight: "#f56954",
        label: "Pending Fund"
    }
];

这是我尝试过的脚本:它打印未定义的值。

var total_value='';
for(var i=0;i<PieData.length;i++){
   $.each(PieData[i], function (index, val) {
       total_value += val.value;
   });
}
alert(total_value);

5 个答案:

答案 0 :(得分:7)

您可以使用原生方法Array#reduce

&#13;
&#13;
var PieData = [{ value: 25515512, color: "#00a65a", highlight: "#00a65a", label: "Received Fund" }, { value: 916952499, color: "#f56954", highlight: "#f56954", label: "Pending Fund" }],
    sum = PieData.reduce(function (s, a) {
        return s + a.value;
    }, 0);

console.log(sum);
&#13;
&#13;
&#13;

ES6

&#13;
&#13;
var PieData = [{ value: 25515512, color: "#00a65a", highlight: "#00a65a", label: "Received Fund" }, { value: 916952499, color: "#f56954", highlight: "#f56954", label: "Pending Fund" }],
    sum = PieData.reduce((s, a) => s + a.value, 0);

console.log(sum);
&#13;
&#13;
&#13;

答案 1 :(得分:3)

您可以使用javascript forEach() 方法,如下所示。

 var PieData = [
           {
               value: 25515512,
               color: "#00a65a",
               highlight: "#00a65a",
               label: "Received Fund"

           },
           {
               value: 916952499,
               color: "#f56954",
               highlight: "#f56954",
               label: "Pending Fund"
           }
    ];

var sum = 0;
PieData.forEach(function(item){
    sum += item.value;
})

console.log(sum)

答案 2 :(得分:2)

要改变的事情:

  • 初始化总计为0,因为字符串上的+运算符连接了这些值。
  • $.each遍历传递的对象,因此您可以直接在回调中访问它以计算总和。

示例代码段:

&#13;
&#13;
 var PieData = [{
   value: 25515512,
   color: "#00a65a",
   highlight: "#00a65a",
   label: "Received Fund"

 }, {
   value: 916952499,
   color: "#f56954",
   highlight: "#f56954",
   label: "Pending Fund"
 }];

 //calculating total
 var total = 0;
 $.each(PieData, function(index, value) {
   total += value.value;
 })
 alert(total)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

PieData是一个包含2个元素的数组,每个元素都是一个HashTable。 你可以通过说:

来总结它们
var sum = PieData[0]["value] + PieData[1]["value"]

如果您有更多元素或只是想使用循环:

var sum=0;
for(var i=0;i<PieData.length;i++){
   sum+=PieData[i]["value"];
}

答案 4 :(得分:0)

&#13;
&#13;
var PieData = [{
        value: 25515512,
        color: "#00a65a",
        highlight: "#00a65a",
        label: "Received Fund"
    }, {
        value: 916952499,
        color: "#f56954",
        highlight: "#f56954",
        label: "Pending Fund"
    }],
    userSelectedColors = ['value'];

var totalCount = _.sumBy(userSelectedColors, _.partial(_.sumBy, PieData));

console.log(totalCount);
&#13;
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>
&#13;
&#13;
&#13;