简体   繁体   English

总结对象中的所有属性

[英]Sum all properties in object

I have the following object:我有以下对象:

{"speed":299,"equipment":49,"teleabb":49,"additional":50,"optional":"299"}

I want to sum all this values and print It out.我想总结所有这些值并打印出来。 How can I sum the properties values?如何总结属性值? :) :)

  1. Iterate over the object properties using for(var in)使用for(var in)迭代对象属性
  2. Use parseInt since some of the integers are in string form使用parseInt因为一些整数是字符串形式

 var obj = {"speed":299,"equipment":49,"teleabb":49,"additional":50,"optional":"299"}; var sum = 0; for(var key in obj){ sum += parseInt(obj[key]); } document.write(sum);

Here's a way of doing it using ES5's Object.keys and reduce :这是使用 ES5 的Object.keysreduce的一种方法:

 var obj = {"speed":299,"equipment":49,"teleabb":49,"additional":50,"optional":"299"}; var sum = Object.keys(obj).reduce(function(prev, current, index) { return prev + (+obj[current]); }, 0); console.log(sum); // 746

jsFiddle js小提琴

My for-less version:我的 for-less 版本:

 var obj = {"speed":299,"equipment":49,"teleabb":49,"additional":50,"optional":"299"}; function sumProperties(obj) { return Object.getOwnPropertyNames(obj) .map(function(item){ return +obj[item];}) .reduce(function(acc,item) { return acc + item; }); } document.write(sumProperties(obj));

Given给定的

var obj = {"speed":299,"equipment":49,"teleabb":49,"additional":50,"optional": 299}

you can do it very easily with the lodash library:你可以用lodash库很容易地做到这一点

var result = _.sum(obj);

If some of your values aren't numbers, you need to map them to numbers first:如果您的某些值不是数字,则需要先将它们映射到数字:

var result = _.sum(_.map(obj, function(n){
     return +n;
     //or parseInt(n) or parseInt(n,10)
}));

http://plnkr.co/edit/UQs1aTCJ8qe1kG15G4x7?p=preview http://plnkr.co/edit/UQs1aTCJ8qe1kG15G4x7?p=preview

You can do it this way:你可以这样做:

var sum_object = {"speed":299,"equipment":49,"teleabb":49,"additional":50,"optional":"299"};
var sum = 0;

for( var index in sum_object ){
    sum += parseInt(sum_object[index]);
    console.log("Val: ",sum_object[index], sum);
};

console.log(sum);

JSFiddle: http://jsfiddle.net/abvuh5m0/ JSFiddle: http : //jsfiddle.net/abvuh5m0/

var obj = {"speed":299,"equipment":49,"teleabb":49,"additional":50,"optional":"299"};


function sum(obj){
    var sum = 0;
    for(var key in obj){
      if (obj. hasOwnProperty(key)) {
          sum += parseInt(obj[key]) || 0;  
       }
    }
    return sum
}

console.log(sum(obj));

parseInt(obj[key]) || 0; is important id the value is not a number重要 id 值不是数字

Using Lodash:使用 Lodash:

var obj = {"speed":299,"equipment":49,"teleabb":49,"additional":50,"optional":"299"};

var val = _.sumBy(_.values(obj), function(v) {
   return parseInt(v)
});

Example: https://codepen.io/dieterich/pen/dyymoxM?editors=0012示例: https : //codepen.io/dieterich/pen/dyymoxM?editors=0012

var sum = 0;
for(var key in objects) {
    sum += parseInt(objects[key]);
}
console.log(sum);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM