How to calculate sum of object keys in array - javascript

前端 未结 5 1145
甜味超标
甜味超标 2021-02-09 11:50

Question

I am working with firebase and react native.

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

[Object, Object,         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-02-09 12:18

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

提交回复
热议问题