0

I want to add sum of first, second and third place to all teams after filtering the basic data.
But I am stuck with data which I want to push and I don't know how to fix this. Every time when function find a team with first position on specific season I want to add this fact to table medalTables to specific team f.e in season 2012 Barca was first and I want to add this to {team: Barca, first: 1} etc. (data are only example)

Code:

let medalTeams = [{
  name: "Real",
  first: 0,
  second: 0,
  third: 0
},{
  name: "Barca",
  first: 0,
  second: 0,
  third: 0
},{
  name: "Atletico",
  first: 0,
  second: 0,
  third: 0
}
 ]

  const medals = tables
    .filter((team) => (team.position === 1 || team.position === 2 || team.position === 3))
    .map((team, index) => {
        if(team.position === 1)
        medalTeams.filter(team => team.name === team.club).push({first: 1})
     
    }); 

What I want to reach:

/*
let medalTeams = [{
  name: "Real",
  first: 3,
  second: 1,
  third: 0
},{
  name: "Barca",
  first: 1,
  second: 2,
  third: 1
},{
  name: "Atletico",
  first: 0,
  second: 1,
  third: 3
}
 ]
 */

Basic data:

const tables = [
  {
    season: 2011,
    position: 1,
    club: 'Real'
  },
  {
    season: 2011,
    position: 2,
    club: 'Barca'
  },
  {
    season: 2011,
    position: 3,
    club: 'Atletico'
  },
    {
    season: 2012,
    position: 2,
    club: 'Real'
  },
  {
    season: 2012,
    position: 1,
    club: 'Barca'
  },
  {
    season: 2012,
    position: 3,
    club: 'Atletico'
  },
    {
    season: 2013,
    position: 1,
    club: 'Real'
  },
  {
    season: 2013,
    position: 3,
    club: 'Barca'
  },
  {
    season: 2013,
    position: 2,
    club: 'Atletico'
  },
    {
    season: 2014,
    position: 1,
    club: 'Real'
  },
  {
    season: 2014,
    position: 2,
    club: 'Barca'
  },
  {
    season: 2014,
    position: 3,
    club: 'Atletico'
  },
]

2 Answers 2

2

Use a switch for the position and increment either the first, second, or third place field.

const tables = [
  { season: 2011 ,  position: 1 , club: 'Real'     },
  { season: 2011 ,  position: 2 , club: 'Barca'    },
  { season: 2011 ,  position: 3 , club: 'Atletico' },
  { season: 2012 ,  position: 2 , club: 'Real'     },
  { season: 2012 ,  position: 1 , club: 'Barca'    },
  { season: 2012 ,  position: 3 , club: 'Atletico' },
  { season: 2013 ,  position: 1 , club: 'Real'     },
  { season: 2013 ,  position: 3 , club: 'Barca'    },
  { season: 2013 ,  position: 2 , club: 'Atletico' },
  { season: 2014 ,  position: 1 , club: 'Real'     },
  { season: 2014 ,  position: 2 , club: 'Barca'    },
  { season: 2014 ,  position: 3 , club: 'Atletico' }
];

let medalTeams = [
  { name: "Real"     , first: 0 , second: 0 , third: 0 },
  { name: "Barca"    , first: 0 , second: 0 , third: 0 },
  { name: "Atletico" , first: 0 , second: 0 , third: 0 }
];

tables.forEach(entry => {
  const found = medalTeams.find(team => team.name === entry.club);
  switch (entry.position) {
    case 1: found.first++  ; break;
    case 2: found.second++ ; break;
    case 3: found.third++  ; break;
  }
});

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

You can make this more dynamic by adding the medal teams dynamically, based on the data.

const tables = [
  { season: 2011 , position: 1 , club: 'Real'     },
  { season: 2011 , position: 2 , club: 'Barca'    },
  { season: 2011 , position: 3 , club: 'Atletico' },
  { season: 2012 , position: 2 , club: 'Real'     },
  { season: 2012 , position: 1 , club: 'Barca'    },
  { season: 2012 , position: 3 , club: 'Atletico' },
  { season: 2013 , position: 1 , club: 'Real'     },
  { season: 2013 , position: 3 , club: 'Barca'    },
  { season: 2013 , position: 2 , club: 'Atletico' },
  { season: 2014 , position: 1 , club: 'Real'     },
  { season: 2014 , position: 2 , club: 'Barca'    },
  { season: 2014 , position: 3 , club: 'Atletico' }
];

const calculateMedals = (data) => {
  return data.reduce((result, entry) => {
    let team = result.find(existing => existing.name === entry.club);
    if (!team) {
      team = { name: entry.club, first: 0, second: 0, third: 0 };
      result.push(team);
    };
    switch (entry.position) {
      case 1: team.first++  ; break;
      case 2: team.second++ ; break;
      case 3: team.third++  ; break;
    }
    return result;
  }, []);
};

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

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

Comments

1

This is really just a groupBy and sum situation.

Here is a general solution grouping by club and summing positions. It is a reduce() call, summing positions using position as key, and then remapping the positions to string keys.

const tables = [{ season: 2011, position: 1, club: 'Real' }, { season: 2011, position: 2, club: 'Barca' }, { season: 2011, position: 3, club: 'Atletico' }, { season: 2012, position: 2, club: 'Real' }, { season: 2012, position: 1, club: 'Barca' }, { season: 2012, position: 3, club: 'Atletico' }, { season: 2013, position: 1, club: 'Real' }, { season: 2013, position: 3, club: 'Barca' }, { season: 2013, position: 2, club: 'Atletico' }, { season: 2014, position: 1, club: 'Real' }, { season: 2014, position: 2, club: 'Barca' }, { season: 2014, position: 3, club: 'Atletico' },];

const byClub = Object.values(
  tables.reduce((a, { club, position, season }) => (
    (a[club] ??= { name: club, 1: 0, 2: 0, 3: 0 })[position] += 1, a), {})
).map(({ name, 1: first, 2: second, 3: third }) => ({ name, first, second, third }));

console.log(byClub);

Note: the snippet uses the lnullish assignment (??=) operator, but this can easily be replaced with a standard || short circuit.

(a[club] = a[club] || { 1: 0, 2: 0, 3: 0 })[position] += 1, a), {})

Comments

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.