2

What is the best way to sum an object array of varying length that looks like this:

data = [{"category":"category1","months":{"1":34,"2":67,"3":29,...}
{"category":"category2","months":{"1":34,"2":627,"3":292,...}
{"category":"category3","months":{"1":46,"2":665,"3":129,...}
{"category":"category4","months":{"1":624,"2":667,"3":629,...}
{"category":"category5","months":{"1":32,"2":637,"3":299,...}
}]

I want to basically group by month and sum and assign that to a total category. So I would get a new object that looks like the following that I would push to my data object:

{"category":"total","months":{"1":770,"2":2663,"3":1378,...}

I would end up with:

data = [{"category":"category1","months":{"1":34,"2":67,"3":29,...}
{"category":"category2","months":{"1":34,"2":627,"3":292,...}
{"category":"category3","months":{"1":46,"2":665,"3":129,...}
{"category":"category4","months":{"1":624,"2":667,"3":629,...}
{"category":"category5","months":{"1":32,"2":637,"3":299,...}
{"category":"total","months":{"1":770,"2":2663,"3":1378,...}
}]

2 Answers 2

1
let data = [
{"category":"category1","months":{"1":34,"2":67,"3":29}},
{"category":"category2","months":{"1":34,"2":627,"3":292}},
{"category":"category3","months":{"1":46,"2":665,"3":129}},
{"category":"category4","months":{"1":624,"2":667,"3":629}},
{"category":"category5","months":{"1":32,"2":637,"3":299}},
];

let total = {"category":"total","months":{}};

data.forEach(category => {
      for(let prop in category.months){
          if (total.months[prop]){
            total.months[prop] += category.months[prop];
          }
          else{
            total.months[prop] = category.months[prop];
          }
      }
});

data.push(total);
Sign up to request clarification or add additional context in comments.

Comments

1

Use Array.prototype.reduce and a hash table to get the 'total' object and later on push it back to the data to obtain the data structure that you want.

See demo below:

var data=[{"category":"category1","months":{"1":34,"2":67,"3":29}},{"category":"category2","months":{"1":34,"2":627,"3":292}},{"category":"category3","months":{"1":46,"2":665,"3":129}},{"category":"category4","months":{"1":624,"2":667,"3":629}},{"category":"category5","months":{"1":32,"2":637,"3":299}}]

var result = data.reduce(function(hash){
  return function(p,c){
    Object.keys(c.months).forEach(function(e){
      if(e in p.months) {
        p.months[e] += c.months[e];
      } else {
        p.months[e] = +c.months[e];
      }
    });
    return p;
  }
}(Object.create(null)), {category:"total",months:{}});

console.log(result);

// now push this to data
data.push(result);

// console.log(data);
.as-console-wrapper{top:0;max-height:100%!important;}

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.