求和对象中的所有属性 [英] Sum all properties in object

查看:107
本文介绍了求和对象中的所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下对象:

{"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. 使用迭代对象属性for(var in )

  2. 使用 parseInt ,因为某些整数是字符串形式

  1. Iterate over the object properties using for(var in)
  2. Use parseInt since some of the integers are in string form

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

这篇关于求和对象中的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆