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

6
  • Can this solve it: stackoverflow.com/questions/40774697/… Commented Feb 4, 2022 at 9: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 Commented Feb 4, 2022 at 9:37
  • @JeremyThille the question has been asked thousands of times, theres no point answering it again. Commented Feb 4, 2022 at 9:39
  • That's precisely why I didn't answer it. I guess there's no harm in giving hints though. Commented Feb 4, 2022 at 9:39
  • @Jamiec thanks for the information mate, like I said, maybe I didn't search correctly. I'll try again Commented Feb 4, 2022 at 9:42

1 Answer 1

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)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.