0

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 :

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

lokomate
  • 21
  • 2
  • Can this solve it: https://stackoverflow.com/questions/40774697/how-can-i-group-an-array-of-objects-by-key – Bjop Feb 04 '22 at 09:36
  • 1
    You probably need to use loops, like `for()`, and also `push()` objects to a new empty array, things like that. It's classic data manipulation. Please give it a go, show your attempt, and if you're stuck at a particular point, we can help – Jeremy Thille Feb 04 '22 at 09:37
  • @JeremyThille the question has been asked thousands of times, theres no point answering it _again_. – Jamiec Feb 04 '22 at 09:39
  • That's precisely why I didn't answer it. I guess there's no harm in giving hints though. – Jeremy Thille Feb 04 '22 at 09:39
  • @Jamiec thanks for the information mate, like I said, maybe I didn't search correctly. I'll try again – lokomate Feb 04 '22 at 09:42
  • Looks like a fun homework assignment. What course are you taking. Here’s a hint. You are going to want to traverse through that entire first array. Grab first object, look at it. Ask question, _Have I seen this location before?_. If not, do something. If so, do something different – zipzit Feb 04 '22 at 09:44

1 Answers1

0

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)
Debug Diva
  • 26,058
  • 13
  • 70
  • 123