How to set default value to set Map and/or then value++ for each in JS

Viewed 111
const salaries = {
  Progger: {salary: 1000, tax: '15%'},
  Tester: {salary: 800, tax: '10%'},
  Manager: { salary: 1000, tax: '10%' },
  Designer: { salary: 600, tax: '30%' },
  Artist: { salary: 1500, tax: '15%' }
};

const team = [
  { name: 'Vasya', specialization: 'Tester' },
  { name: 'Taras', specialization: 'Tester' },
  { name: 'Taras I', specialization: 'Tester' },
  { name: 'Tapas', specialization: 'Humor' },
  { name: 'Misha', specialization: 'Manager' },
  { name: 'Max', specialization: 'Designer' },
  { name: 'Vova', specialization: 'Designer' },
  { name: 'Leo', specialization: 'Artist' }
];

function calculateTeamFinanceReport(salaries, team) {
  const proffNames = Object.keys(salaries);
  let proffesions = new Map();
  let result = {};
  let value;

  // for (let every of proffNames) {
  //   proffesions.set(every, 0)
  // }

  for (let i = 0; i < team.length; i++) {
    let specialization = team[i].specialization;
    if (proffNames.some(proffesion => {   //specialisation has to be salaries `team.specialization == salaries[any]`
      if (specialization === proffesion) {
        value = value || 0;
        proffesions.set(proffesion, value++); // Right count here needed
      }
    }))
      i++;
  }
  return proffesions
}

Result = 'Progger' => 1, 'Tester' => 4, 'Manager' => 5, 'Designer' => 7, 'Artist' => 8

I need to count here how much people have exact profession. Instead I get total count. Also specialisation from team has to be in salaries, else = ignore and continue.

team.specialization == salaries[any]

How to get right count for each profession? Or how to set default value 1 for proffesions map object an then ++ it?

2 Answers
  • The value is salaries[proffesion]['salary']
  • If there isn't a proffesion in the dept object (result = {} originally), then add it to dept object and .set() it in proffesions Map.
  • Then .get() the proffesion from proffesions Map and add the value to the salary in dept[proffesion]
  • Finally .set() the new value to proffesions Map.

const salaries={Programmer:{salary:1e3,tax:"15%"},Tester:{salary:800,tax:"10%"},Manager:{salary:1e3,tax:"10%"},Designer:{salary:600,tax:"30%"},Executive:{salary:1500,tax:"15%"}},team=[{name:"Vasya",specialization:"Tester"},{name:"Taras",specialization:"Tester"},{name:"Taras I",specialization:"Tester"},{name:"Tapas",specialization:"Programmer"},{name:"Misha",specialization:"Manager"},{name:"Max",specialization:"Designer"},{name:"Vova",specialization:"Designer"},{name:"Leo",specialization:"Executive"},
{name:"Ivan",specialization:"Unemployed"}];

function calculateTeamFinanceReport(salaries, team) {
  const proffNames = Object.keys(salaries);
  let proffesions = new Map();
  let dept = {};
  let value = 0;

  for (let i = 0; i < team.length; i++) {
    let specialization = team[i].specialization;
    proffNames.forEach(proffesion => {
      if (specialization === proffesion) {
        value = salaries[proffesion]['salary'] || 0;
        if (!dept[proffesion]) {
          proffesions.set(proffesion, 0);
          dept[proffesion] = 0;
        }
        dept[proffesion] = value;
        let v = proffesions.get(proffesion);
        value = v + value;
        proffesions.set(proffesion, value);
      }
    });
  }
  return proffesions
}

console.log(Array.from(calculateTeamFinanceReport(salaries, team).entries()));

function totalMembers(teams, selectedTeam) {
     return teams.filter(team => team === selectedTeam)?.length;
 }

this function will return the total members for the provided team in teams array.

function calculateTeamFinanceReport(salaries, team) {
    const teamHashTable = {};
    let totalMember = 1;
    for (const specialization in salaries) {
        const teamSpecializations = team.map(t => t.specialization);
        totalMember += totalMembers(teamSpecializations, specialization);
        teamHashTable[specialization] = totalMember;
    }
    return teamHashTable;
}

Taking the totalMember for the specialization then saving that for the specialization and then starts from there for next turn and so on.

Output: enter image description here

Hope this answer would help!

Related