-3

I have the following array:

var curr = [{ "Year": 2019, "Title": "Asset Management Sample", "Sum": 1020000.0, "Budget":0

}, {
  "Year": 2019,
  "Title": "Monday test 2",
  "Sum": 2546658.0,
  "Budget":100
}, {
  "Year": 2020,
  "Title": "Asset Management Sample",
  "Sum": 1020000.0,
  "Budget":1000
}, {
  "Year": 2020,
  "Title": "Monday test 2",
  "Sum": 3472000.0,
  "Budget":100
}, {
  "Year": 2021,
  "Title": "Asset Management Sample",
  "Sum": 1020000.0,
  "Budget":100
}, {
  "Year": 2021,
  "Title": "Monday test 2",
  "Sum": 2452000.0,
  "Budget":100
}]

That I need to change to:

[{
  "Year": 2019,
  "Asset Management Sample": 1020000.0,
  "Monday test": 2546658.0
}, {
  "Year": 2020,
  "Asset Management Sample": 1020000.0,
  "Monday test 2": 3472000.0
}, {
  "Year": 2021,
  "Asset Management Sample": 1020000.0,
  "Monday test 2": 2452000.0
}]

With help from earlier posters I have used .reduce (slightly modified from below) to generate this:

      var res = arr.reduce(function(acc, curr) {
      acc[curr.Year] = acc[curr.Year];
      acc[curr.Year] = acc[curr.Year] || { Year: curr.Year } ;
      acc[curr.Year][curr.Title] = curr.Sum;

      return acc;

I need to expand this to include a sum of all the budget values for each year (there should be a single budget value per year). I added the following line in before the return:

acc[curr.Year][curr.Budget] = curr[curr.Budget] || { Budget: curr.Budget } ;

It is adding individual entries for each Budget value. How do I sum the Budget values and return it without affecting the other returned array?

5
  • 6
    Please show your code. Also, there is no groupBy in JavaScript. Commented May 28, 2019 at 8:35
  • Check for answer here: stackoverflow.com/questions/1129216/… Commented May 28, 2019 at 8:35
  • possible duplicate of stackoverflow.com/questions/39302871/… Commented May 28, 2019 at 8:35
  • Please edit your question and show the current code. Commented May 28, 2019 at 8:36
  • You want to group by year or title? Commented May 28, 2019 at 8:37

1 Answer 1

2

Use reduce like so:

const arr = [{"Year":2019,"Title":"Asset Management Sample","Sum":1020000},{"Year":2019,"Title":"Monday test 2","Sum":2546658},{"Year":2020,"Title":"Asset Management Sample","Sum":1020000},{"Year":2020,"Title":"Monday test 2","Sum":3472000},{"Year":2021,"Title":"Asset Management Sample","Sum":1020000},{"Year":2021,"Title":"Monday test 2","Sum":2452000}];
const res = Object.values(arr.reduce((acc, { Year, Title, Sum }) => (acc[Year] = acc[Year] || { Year }, acc[Year][Title] = Sum, acc), {}));
console.log(res);

More verbose version:

const arr = [{"Year":2019,"Title":"Asset Management Sample","Sum":1020000},{"Year":2019,"Title":"Monday test 2","Sum":2546658},{"Year":2020,"Title":"Asset Management Sample","Sum":1020000},{"Year":2020,"Title":"Monday test 2","Sum":3472000},{"Year":2021,"Title":"Asset Management Sample","Sum":1020000},{"Year":2021,"Title":"Monday test 2","Sum":2452000}];
const res = Object.values(arr.reduce((acc, { Year, Title, Sum }) => {
  acc[Year] = acc[Year] || { Year };
  acc[Year][Title] = Sum;
  return acc;
}, {}));
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

ES5 syntax:

var arr = [{"Year":2019,"Title":"Asset Management Sample","Sum":1020000},{"Year":2019,"Title":"Monday test 2","Sum":2546658},{"Year":2020,"Title":"Asset Management Sample","Sum":1020000},{"Year":2020,"Title":"Monday test 2","Sum":3472000},{"Year":2021,"Title":"Asset Management Sample","Sum":1020000},{"Year":2021,"Title":"Monday test 2","Sum":2452000}];
var res = arr.reduce(function(acc, curr) {
  acc[curr.Year] = acc[curr.Year] || { Year: curr.Year };
  acc[curr.Year][curr.Title] = curr.Sum;
  return acc;
}, {});
res = Object.keys(res).map(function(key) {
  return res[key];
});
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

Sign up to request clarification or add additional context in comments.

7 Comments

It's okay to use semicolons, new lines and return. This is not code-golf :) It'll be very hard for the OP to understand what's going on here
My bad @adiga :) I'll add a more verbose version.
Edited @adiga is that understandable :D
Object.values() is ES2016+ (Not even 2015)
Welp, missed that.
|

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.