Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SUM AND GROUPING JSON DATA USING JAVASCRIPT

Sorry if this has been asked before, but I couldn’t find a good example of what I’m trying to accomplish. Maybe I’m just not searching for the right thing. Please correct me if there’s an explanation of this somewhere.

so let’s says I have a data like this :

data = [
{"no":1,"location":"New York","transaction":3000},
{"no":2,"location":"Tokyo","transaction":3000},
{"no":3,"location":"New York","transaction":3000},
{"no":4,"location":"Amsterdam","transaction":3000},
{"no":5,"location":"Manchester","transaction":3000},
{"no":6,"location":"New York","transaction":3000},
{"no":7,"location":"Tokyo","transaction":3000},
{"no":8,"location":"Tokyo","transaction":3000},
{"no":9,"location":"New York","transaction":3000},
{"no":10,"location":"Amsterdam","transaction":3000}
]

what i wanted to is an output like this :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

result = [
{"location":"New York","transaction":12000},
{"location":"Tokyo","transaction":9000},
{"location":"Amsterdam","transaction":6000}
{"location":"Manchester","transaction":3000}
]

so what i wanted to do is grouping the data based on location and sum the transaction where the location is same and push the data to another array. i don’t know where to start, need some help to solve this or any suggestion to solve this using Javascript. thank you

>Solution :

Working Demo :

// Input array
const data = [
{"no":1,"location":"New York","transaction":3000},
{"no":2,"location":"Tokyo","transaction":3000},
{"no":3,"location":"New York","transaction":3000},
{"no":4,"location":"Amsterdam","transaction":3000},
{"no":5,"location":"Manchester","transaction":3000},
{"no":6,"location":"New York","transaction":3000},
{"no":7,"location":"Tokyo","transaction":3000},
{"no":8,"location":"Tokyo","transaction":3000},
{"no":9,"location":"New York","transaction":3000},
{"no":10,"location":"Amsterdam","transaction":3000}
];

// result array
const resultArr = [];

// grouping by location and resulting with an object using Array.reduce() method
const groupByLocation = data.reduce((group, item) => {
  const { location } = item;
  group[location] = group[location] ?? [];
  group[location].push(item.transaction);
  return group;
}, {});

// Finally calculating the sum based on the location array we have.
Object.keys(groupByLocation).forEach((item) => {
    groupByLocation[item] = groupByLocation[item].reduce((a, b) => a + b);
    resultArr.push({
        'location': item,
      'transaction': groupByLocation[item]
    })
})

console.log(resultArr)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading