Creating an array of cumulative sum in javascript

后端 未结 21 1518
时光取名叫无心
时光取名叫无心 2020-11-27 06:23

This is an example of what I need to do:

var myarray = [5, 10, 3, 2];

var result1 = myarray[0];
var result2 = myarray[1] + myarray[0];
var result3 = myarray         


        
相关标签:
21条回答
  • 2020-11-27 07:15

    Returns sorted obj by key and sorted array!!!

    var unsorted_obj = {
      "2016-07-01": 25,
      "2016-07-04": 55,
      "2016-07-05": 84,
      "2016-07-06": 122,
      "2016-07-03": 54,
      "2016-07-02": 43
    };
    
    var sort_obj = function(obj){
      var keys = [];
      var sorted_arr = [];
      var sorted_obj = {};
    
      for(var key in obj){
        if(obj.hasOwnProperty(key)){
          keys.push(key);
        }
      }
    
      keys.sort();
    
      jQuery.each(keys, function(i, key){
        sorted_obj[key] = obj[key];
        var val = obj[key];
        sorted_arr.push({
          idx: i,
          date: key,
          val: val
        })
      });
    
      return { sorted_obj: sorted_obj, sorted_arr: sorted_arr };
    
    };
    
    var sorted_obj = sort_obj(unsorted_obj).sorted_obj;
    var sorted_arr = sort_obj(unsorted_obj).sorted_arr;
    
    // sorted_arr = [{"idx":0,"date":"2016-07-01","val":25},{"idx":1,"date":"2016-07-02","val":43},{"idx":2,"date":"2016-07-03","val":54},...]
    // sorted_obj = {"2016-07-01":25,"2016-07-02":43,"2016-07-03":54,...}
    
    0 讨论(0)
  • 2020-11-27 07:17

    /*Checkout the explanation below */

    nums = [1,2,3,4]
    var runningSum = function(nums) { 
    shoppingCart =[];
    runningtotal =0;  
    nums.forEach(EachValue => {  
    runningtotal += EachValue
    shoppingCart.push(runningtotal);  
    });
    return shoppingCart       
    };
    
    console.log(runningSum(nums));
    

    /* define some numbers*/

    nums = [1,2,3,4]
    

    /* assign function runningSum , some numbers*/

    var runningSum = function(nums) { 
    shoppingCart =[]; /* Create a empty shopping cart to store the items */
    runningtotal =0;  /* Start with your beginning bill of zero items in the cart */
    

    /* remove a number from list of numbers call each one of the numbers from the array => EachValue using a pointer function*/

    nums.forEach(EachValue => {  
    
             (runningtotal += EachValue) 
    

    /* Now add the value to the runningtotal to represent items prices*/

    shoppingCart.push(runningtotal);  
    

    /* Use the push method to place it into the new array called shopping cart */ });

    return shoppingCart

    /* output the items currently in the shopping cart with only 1d prices */

    };

        nums = [1,2,3,4]
        var runningSum = function(nums) { 
        shoppingCart =[];
        runningtotal =0;  
        nums.forEach(EachValue => {  
        runningtotal += EachValue
        shoppingCart.push(runningtotal);  
        });
        return shoppingCart       
        };
    
        console.log(runningSum(nums));

    0 讨论(0)
  • 2020-11-27 07:18

    A couple more options with ES6 array spreading

    [1, 2, 3].reduce((a, x, i) => [...a, x + (a[i-1] || 0)], []); //[1, 3, 6]
    

    or

    [3, 2, 1].reduce((a, x, i) => [...a, a.length > 0 ? x + a[i-1] : x], []); //[3, 5, 6]
    
    0 讨论(0)
提交回复
热议问题