如何计算数组中对象键的总和 - javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39402442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate sum of object keys in array - javascript
提问by gbland777
Question
题
I am working with firebase and react native.
我正在使用 firebase 并做出本机反应。
I have returned an array from my firebase database that looks like this.
我从我的 firebase 数据库中返回了一个数组,看起来像这样。
[Object, Object, Object]
Under each object I have returned a single item, "level:4".
在每个对象下,我返回了一个项目,“级别:4”。
So I have three objects containing 4,5,6. How do I sum these together?
所以我有三个包含 4,5,6 的对象。我如何总结这些?
Thanks!
谢谢!
回答by Yasin Yaqoobi
You can use Javascript's reduce function. This is basically the same as @epascarello answer but in ES6 syntax.
您可以使用 Javascript 的 reduce 功能。这与@epascarello 的答案基本相同,但采用 ES6 语法。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
const arr = [{level:2},{level:4},{level:5}];
const total = arr.reduce((prev,next) => prev + next.level,0);
console.log(total);
回答by epascarello
Either a simple loop
要么是一个简单的循环
var a = [{level:1},{level:2},{level:3},{level:4}],
total = 0;
for (var i=0; i<a.length; i++) {
total += a[i].level;
}
console.log('total', total)
or reduce
或减少
var a = [{level:1},{level:2},{level:3},{level:4}]
console.log(a.reduce( function(cnt,o){ return cnt + o.level; }, 0))
回答by Prateek Gupta
Use Array.prototype.forEach()
method to iterate over your array and sum your elements.
使用Array.prototype.forEach()
方法遍历数组并对元素求和。
Let us suppose your array is var foo = [object, object, object]
Each object
has this structure, { level : 4 }
让我们假设你的数组是var foo = [object, object, object]
Eachobject
有这样的结构,{ level : 4 }
Write code like this:
像这样写代码:
var sum = 0;
foo.forEach(function(obj){
sum += obj.level;
});
sum
will store the sum
sum
将存储总和
回答by Joey Wood
Have you tried accessing the object in the array and calling the property?
您是否尝试过访问数组中的对象并调用该属性?
var array = [object, object, object];
var sum = array[0].level + array[1].level + array[2].level;
回答by Javidan Akberov
This code will calculate the sum of an array. First, declare one variable; this variable's name will be sum
. sum
contains the total's sum. Second, declare a numbers
variable of type Array
. This variable will contain digits.
此代码将计算数组的总和。首先声明一个变量;此变量的名称将为sum
. sum
包含总和。其次,声明一个numbers
类型为 的变量Array
。此变量将包含数字。
Then we start a loop (for) operation, in which we assign to the sum
variable the value of (sum= sum+i);
.
Then we show in (document.write)
the numbers and sum.
然后我们开始一个循环 (for) 操作,在该操作中我们将sum
的值分配给变量(sum= sum+i);
。然后我们显示(document.write)
数字和总和。
var summa = 0 , i ;
var numbers= [1,2,3,4,5];
for(i = 0; i <= numbers.length; i++){
summa=summa+i;
}
document.write(numbers , ": bu reqemlerin cemi " ,"=" , summa);