简体   繁体   English

如何从javascript中的对象数组中获取键值的总和?

[英]How do I get the sum of the values of a key from array of objects in javascript?

I need to sum all of the values of a key "number" from several objects within an array. 我需要对数组中多个对象的键“数字”的所有值求和。 What would be the correct way to do this? 正确的方法是什么? This is what I've tried. 这就是我尝试过的。

var numarray = [{"number":"10"},{"number":"2"},{"number":"5"},
{"number":"3"},{"number":"21"},{"number":"43"},{"number":"30"}];

function Sum() {
  var sum;

  for (number in numarray[i]){
    alert(sum); //sum would be the result I need to get
  }
}

Use forEach to loop through each of the json object. 使用forEach遍历每个json对象。

Use parseInt to convert the values to integer 使用parseInt将值转换为整数

var numarray = [
{"number":"10"},
{"number":"2"},
{"number":"5"},
{"number":"3"},
{"number":"21"},
{"number":"43"},
{"number":"30"}];

function Sum(){
var sum=0;

numarray.forEach(function(item){
 sum += parseInt(item.number)
})
document.write('<pre>'+sum+'</pre>')
}
Sum()

DEMO 演示

EDIT 编辑

When using parseInt it is better to specify the radix 使用parseInt时,最好指定基数

parseInt(item.number,10)

Using a 10 radix means the number is parsed with a base 10 and thus turns the number into the integer .If parseInt detects a leading zero it will parse the number in octal base 使用10的基数表示该数字以10为底进行解析,从而将数字转换为整数。如果parseInt检测到前导零,则将以八进制为基础解析该数字

Using Number 使用号码

Number(item.number)

It will not detect any any octal 它不会检测任何八进制

You can use reduce for this: 您可以为此使用reduce

Here's how you would normally find the sum of elements in an array: 通常,这是在数组中查找元素总和的方式:

var sum = arr.reduce(function(a,b) {
  return a + b;
});

Since you have an array of objects, you'll need to change this to something like: 由于您具有对象数组,因此需要将其更改为以下内容:

var sum = arr.reduce(function(a,b) {
  return parseFloat(a) + parseFloat(b.number);
}, 0);

Notice the 0 I've added added as a parameter? 注意到我添加的0作为参数添加了吗? It's the default parameter and required in this case. 这是默认参数,在这种情况下是必需参数。

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

相关问题 如何通过键减少对象数组并在javascript中求和 - How to reduce an array of objects by key and sum its values in javascript 如何从 JavaScript 对象数组中获取键值对? - How do I get key-value pair from array of JavaScript objects? Javascript 对象数组对项目键的值求和,然后连续求和值 - Javascript array of objects sum values for items key then sum values consecutively 如何从Javascript中的对象列表中获取键的值? - How do I get the value of a key from a list of objects in Javascript? 如何记录Javascript对象列表中的值数组? - How do I log an array of values from a list of objects in Javascript? 如何在对象数组中使用相同的键对值求和? - How to sum values with the same key In an array of objects? 如何从javascript中的对象数组中获取值 - How to get values from array of objects in javascript 如何从PHP数组到Javascript数组获取值? - How do I get values from a PHP array to a Javascript Array? 如何从 typescript 中的数组中获取动态键值 - How do I get dynamic key values from an array in typescript 如何从一个键上包含一个数组的对象数组中获取不同的值并计算 JavaScript? - How to get distinct values from an array of objects which has an Array inside in on one key and count JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM