1

I have an array in the following format that has x and y values with a data field and I am looking to convert into an array that adds up all the individual value over a specific data, and then shows a cumulative value (summing over that date and then adding the value to the previous dates). Eg.

data_array = [
{xvalue: 10, yvalue: 5, date: "12/2"},
{xvalue: 10, yvalue: 5, date: "12/2"},
{xvalue: 8, yvalue: 3, date: "12/3"},
{xvalue: 10, yvalue: 5, date: "12/3"},
{xvalue: 6, yvalue: 1, date: "12/4"},
{xvalue: 20, yvalue: 3, date: "12/4"},
]

New array

new_data = [
{xvalue: 20, yvalue: 10, date: "12/2"}
{xvalue: 38, yvalue: 18, date: "12/3"}
{xvalue: 64, yvalue: 22, date: "12/4"}
]

What will be a way to do this efficiently? Currently I have the following way that works but feel like its not the best implementation

var temp = {}
var obj = null
for (var i=0; i<data_array.length; i++){
    obj = data_array[i];

    if (!temp[obj.date]) {
        temp[obj.date]=obj;
    } 
    else {
        temp[obj.date].xvalue += obj.xvalue;
        temp[obj.date].yvalue += obj.yvalue
    }
}
var result = [];
for (var prop in temp){
    result.push(temp[prop]);
}
console.log("result", result)

for (var i=0; i< result.length; i++){
  if (i!=0){
    result[i].xvalue +=result[i-1].xvalue;
    result[i].yvalue +=result[i-1].yvalue;
  }
}
  • Use the `Array.prototype.reduce` function – Jhecht Dec 30 '19 at 08:27
  • 1
    can you tell us what you've tried please ? – jonatjano Dec 30 '19 at 08:27
  • 2
    Does this answer your question? [Sum javascript object propertyA values with same object propertyB in array of objects](https://stackoverflow.com/questions/19233283/sum-javascript-object-propertya-values-with-same-object-propertyb-in-array-of-ob) – AZ_ Dec 30 '19 at 08:30
  • [Sum similar keys in an array of objects](https://stackoverflow.com/questions/24444738) – adiga Dec 30 '19 at 08:32
  • Hi everyone - I updated my implementation but would love some feedback if there is a better way to do this – zeroanthem86 Dec 30 '19 at 09:11

2 Answers2

3

data_array = [
{xvalue: 10, yvalue: 5, date: "12/2"},
{xvalue: 10, yvalue: 5, date: "12/4"},
{xvalue: 8, yvalue: 3, date: "12/3"},
{xvalue: 10, yvalue: 5, date: "12/2"},
{xvalue: 6, yvalue: 1, date: "12/3"},
{xvalue: 20, yvalue: 3, date: "12/2"},
];
var reduced =  data_array.reduce(function(final,item){
  if(final[item.date]){
    final[item.date] = {cummxvalue: item.xvalue+final[item.date].cummxvalue, cummyvalue: item.yvalue+final[item.date].cummyvalue, date: item.date};
  }else{
    final[item.date] = {cummxvalue: item.xvalue, cummyvalue: item.yvalue, date: item.date};
  }
return final;
},{});
var new_data = Object.values(reduced);
console.log(new_data); 
Geetanjali
  • 458
  • 3
  • 13
1

You may loop through the array and add xvalues and yvalue, and copy into a new array as you go if the elements donot have the same date value. You will also need to sort the array incase it's not already sorted as your first example.

const data_array = [
  {xvalue: 10, yvalue: 5, date: "12/2"},
  {xvalue: 10, yvalue: 5, date: "12/4"},
  {xvalue: 8, yvalue: 3, date: "12/3"},
  {xvalue: 10, yvalue: 5, date: "12/2"},
  {xvalue: 6, yvalue: 1, date: "12/3"},
  {xvalue: 20, yvalue: 3, date: "12/2"}
]

let new_data = [];
let cummxvalue = 0;
let cummyvalue = 0;

data_array.sort( (a, b) => a.date > b.date );

for( const obj of data_array) {
  let o = new_data.find(o => o.date === obj.date);
  cummxvalue += obj.xvalue;
  cummyvalue += obj.yvalue;

  if(!o) {
    new_data.push({cummxvalue: cummxvalue, cummyvalue: cummyvalue, date: obj.date});
  }
  else {
    o.cummxvalue = cummxvalue;
    o.cummyvalue = cummyvalue;
  }
}

console.log(new_data)
Addis
  • 2,480
  • 2
  • 13
  • 21