14

Question

I am working with firebase and react native.

I have returned an array from my firebase database that looks like this.

[Object, Object, Object]

Under each object I have returned a single item, "level:4".

So I have three objects containing 4,5,6. How do I sum these together?

Thanks!

2
  • 4
    loop over it array, read the property, add... Commented Sep 9, 2016 at 1:24
  • Hey gbland777. Welcome to StackOverflow. As you've probably noticed your question got some downvotes. One way to prevent that is to provide (in your question) the minimal code that reproduces the problem. With that we can check your code to see what you might be doing wrong (or need clarification on), instead of trying to backtrace from your description. It's a more efficient for way for people to help; and the easier you make it for us, the more likely you are to get help. :-) Commented Sep 9, 2016 at 1:38

5 Answers 5

24

You can use Javascript's reduce function. This is basically the same as @epascarello answer but in ES6 syntax.

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);
Sign up to request clarification or add additional context in comments.

Comments

20

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

Comments

1

Use Array.prototype.forEach() method to iterate over your array and sum your elements.

Let us suppose your array is var foo = [object, object, object] Each object has this structure, { level : 4 }

Write code like this:

var sum = 0;
foo.forEach(function(obj){
  sum += obj.level;
});

sum will store the sum

Comments

1

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.

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.

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

1 Comment

sorry I am new stackoverflow and I do not know which part of the code is written So I wrote it with the narration
-1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.