JavaScript Reduce method in order to count object values with a specific keys reduced order method

I have an array like this:

const teamsPoints = [
  {team1_game00: 1},
  {team1_game01: 2},
  {team1_game02: 3},
  {team2_game00: 0},
  {team2_game10: 2},
  {team2_game11: 3},
  {team3_game01: 0},
  {team3_game10: 0},
  {team3_game20: 3},
  {team4_game02: 0},
  {team4_game11: 0},
  {team4_game20: 0}
]

What I'm trying to get is:

{
  team1: 6,
  team2: 5,
  team3: 3,
  team4: 0
}

which is the sum of points of every team.

I was trying to achieve that by using reduce method.

const scoreResult = teamsPoints.reduce((total, current) => {    

}, {});

As I understand I'm starting with an empty object, but then I have a problem to get proper key-value pairs (that's why I have not published it here, must admin reduce method is a kinda new thing to me).

Thank you in advance!

Answer:1

Here is a small sample using reduce. I basically get the name of the team, use it as a key in the reduced result and increment its value!

Hope this will help you ;) Feel free to ask me anything if it's not clear!

const teamsPoints = [
{team1_game00: 1},
{team1_game01: 2},
{team1_game02: 3},
{team2_game00: 0},
{team2_game10: 2},
{team2_game11: 3},
{team3_game01: 0},
{team3_game10: 0},
{team3_game20: 3},
{team4_game02: 0},
{team4_game11: 0},
{team4_game20: 0}
];

const scoreResult = teamsPoints.reduce((total, game) => {
  const [gameName] = Object.keys(game);
  const [team] = gameName.split('_');
  total[team] = total[team] || 0;
  total[team] += game[gameName];
  return total;
}, {});

console.log(scoreResult);
Answer:2

You can use the function reduce to group the teams.

const teamsPoints = [{team1_game00: 1},{team1_game01: 2},{team1_game02: 3},{team2_game00: 0},{team2_game10: 2},{team2_game11: 3},{team3_game01: 0},{team3_game10: 0},{team3_game20: 3},{team4_game02: 0},{team4_game11: 0},{team4_game20: 0}],
      result = teamsPoints.reduce((a, c) => {
        let keys = Object.keys(c),
            [key] = keys[0].split('_');
        a[key] = (a[key] || 0) + c[keys[0]];
        return a;
      }, {});

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

So generally I wouldn't use reduce for this, just because of how the objects are organized. You wouldn't really want to signify what game and what team it is in the same object, but if you did have those objects like that I would first create a second object to store the totals, and do the following

let totals = {};

teamsPoints.map(obj => {
  Object.keys(obj).map(key => {
    if (totals.hasOwnProperty(key.split('_')[0])) {
       totals[key.split('_')[0]] += obj[key];
    } else {
      totals[key.split('_')[0]] = obj[key];
    }
  });
});
Answer:4

I have an array object where there are key value pairs. I am trying to get the keys in that array using a loop but I am getting only 0. What is the problem with my code. var strj = '{"name":"John",...

I have an array object where there are key value pairs. I am trying to get the keys in that array using a loop but I am getting only 0. What is the problem with my code. var strj = '{"name":"John",...

  1. cannot open .get/fetch_head
  2. apt-get cannot fetch
  3. fetch cannot get
  4. you-get cannot fetch video
  5. fetch cannot get headers

I'm moving a piece of our programming code from client side to server side because of performance issues (note: not all code is in the post, just the part i'm having trouble with) the specific piece ...

I'm moving a piece of our programming code from client side to server side because of performance issues (note: not all code is in the post, just the part i'm having trouble with) the specific piece ...

I find the functionality of Array.prototype.join very useful because it only applies the join value to the "inner" connections of the elements in the array. Like so: ['Hey', 'there'].join('-') // Hey-...

I find the functionality of Array.prototype.join very useful because it only applies the join value to the "inner" connections of the elements in the array. Like so: ['Hey', 'there'].join('-') // Hey-...

  1. flatmap works

I am using Bootstrap tabs to load a widget from a server. What I need is to load the tab content as soon as the user click the tab instead of the initial load of the page. For ex below is the HTML for ...

I am using Bootstrap tabs to load a widget from a server. What I need is to load the tab content as soon as the user click the tab instead of the initial load of the page. For ex below is the HTML for ...

  1. bootstrap load content
  2. bootstrap load content on scroll
  3. bootstrap load content on tab click
  4. bootstrap load content on click
  5. bootstrap load content in modal
  6. bootstrap load div
  7. bootstrap load tab content ajax
  8. bootstrap load more content
  9. bootstrap load dynamic content
  10. bootstrap load content into div
  11. load a bootstrap popover content with ajax