Javascript cumulate an array of object to an array of object

Question

I have an array of objects:

var myArray = [
    {
        "date" : "03/01/2017",
        "value" : 2
    },  {
        "date" : "04/01/2017",
        "value" : 6
    },  {
        "date" : "05/01/2017",
        "value" : 4
    }
];

I need to cumulate the value and keep the same array with updated values

The result would look like this

var myArray = [
    {
        "date" : "03/01/2017",
        "value" : 2
    },  {
        "date" : "04/01/2017",
        "value" : 8 //(2+6)
    },  {
        "date" : "05/01/2017",
        "value" : 12 //(2+6+4)
    }
];

I am aware that this exists

[0, 1, 2, 3, 4].reduce(function(accumulator, currentValue, currentIndex, array) {
  return accumulator + currentValue;
});

But I cannot find an example with object returning also objects


Show source
| javascript   | arrays   | object   2017-01-04 16:01 4 Answers

Answers to Javascript cumulate an array of object to an array of object ( 4 )

  1. 2017-01-04 16:01

    Use the this argument of Array.prototype.forEach to accumulated the value - see demo below:

    var myArray=[{"date":"03/01/2017","value":2},{"date":"04/01/2017","value":6},{"date":"05/01/2017","value":4}];
    
    myArray.forEach(function(e){
      this.count = (this.count || 0) +  e.value;
      e.value = this.count;
    },Object.create(null));
    
    console.log(myArray);
    .as-console-wrapper{top:0;max-height:100%!important;}

  2. 2017-01-04 16:01

    You can use map() and Object.assign() to make copy of objects.

    var myArray = [{
      "date": "03/01/2017",
      "value": 2
    }, {
      "date": "04/01/2017",
      "value": 6
    }, {
      "date": "05/01/2017",
      "value": 4
    }];
    
    var result = myArray.map(function(o) {
      var obj = Object.assign({}, o)
      obj.value = this.total += o.value
      return obj
    }, {total: 0})
    
    console.log(result)

  3. 2017-01-04 16:01

    Just as a different example and to answer your question on .reduce(), you'd use reduce in this way

    var myArray = [
        {
            "date" : "03/01/2017",
            "value" : 2
        },  {
            "date" : "04/01/2017",
            "value" : 6
        },  {
            "date" : "05/01/2017",
            "value" : 4
        }
    ];
    
    function accumulate() {
      var count = 0;
      return myArray.reduce(function(acc, cur) {
    	count += cur.value || 0;
    	cur.value = count;
    	acc.push(cur);
        return acc;
       }, []);
    }
    
    console.log(accumulate(myArray));

  4. 2017-01-04 17:01

    Simple forEach that mutates the array in place.

    var myArray = [
        {
            "date" : "03/01/2017",
            "value" : 2
        },  
        {
            "date" : "04/01/2017",
            "value" : 6
        },  
        {
            "date" : "05/01/2017",
            "value" : 4
        }
    ];
    
    myArray.forEach(
      (e,i,arr) => e.value += i && arr[i-1].value // only fires if i is truthy (i>0)
    );      
    
    console.log(myArray);
    .as-console-wrapper{top:0;max-height:100%!important;}

Leave a reply to - Javascript cumulate an array of object to an array of object

◀ Go back