Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

94
Visualizações
Checking for same values and update array

I am working with Nodejs and I am stuck on this given below problem and looking for best practices to do it.

I have an array set that would be like this:

[
  { id: 'something1', score: 180, rank: 1, avatar: 'avatar_3', prize: 1280 },
  { id: 'something2', score: 180, rank: 2, avatar: 'avatar_3', prize: 1073 },
  { id: 'something3', score: 161, rank: 3, avatar: 'avatar_2', prize: 902 },
  { id: 'something4', score: 161, rank: 4, avatar: 'avatar_7', prize: 758 },
  { id: 'something5', score: 161, rank: 5, avatar: 'avatar_8', prize: 535 },
  { id: 'something6', score: 161, rank: 6, avatar: 'avatar_3', prize: 318 },
  { id: 'something7', score: 143, rank: 7, avatar: 'avatar_3', prize: 267 },
  { id: 'something8', score: 143, rank: 8, avatar: 'avatar_4', prize: 224 },
  { id: 'something9', score: 140, rank: 9, avatar: 'avatar_3', prize: 210 }
]

and now I want to add all those prizes that have identical scores and then divide them by total players having the same score and then updating the existing array with new prize values.

Here is the sample output that I am looking for:

[
  { id: 'something1', score: 180, rank: 1, avatar: 'avatar_3', prize: 1176 },
  { id: 'something2', score: 180, rank: 2, avatar: 'avatar_3', prize: 1176 },
  { id: 'something3', score: 161, rank: 3, avatar: 'avatar_2', prize: 628},
  { id: 'something4', score: 161, rank: 4, avatar: 'avatar_7', prize: 628},
  { id: 'something5', score: 161, rank: 5, avatar: 'avatar_8', prize: 628},
  { id: 'something6', score: 161, rank: 6, avatar: 'avatar_3', prize: 628},
  { id: 'something7', score: 143, rank: 7, avatar: 'avatar_3', prize: 245},
  { id: 'something8', score: 143, rank: 8, avatar: 'avatar_4', prize: 245},
  { id: 'something9', score: 140, rank: 9, avatar: 'avatar_3', prize: 210 }
]

So what would be the best way to solve this problem? Thanks

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

Build a hash table of totals first (I've used a for...of loop), and then map over the data and replace the prize value of each object with the total that matches the total value in the hash table for that score.

const data=[{id:"something1",score:180,rank:1,avatar:"avatar_3",prize:1176},{id:"something2",score:180,rank:2,avatar:"avatar_3",prize:1176},{id:"something3",score:161,rank:3,avatar:"avatar_2",prize:628},{id:"something4",score:161,rank:4,avatar:"avatar_7",prize:628},{id:"something5",score:161,rank:5,avatar:"avatar_8",prize:628},{id:"something6",score:161,rank:6,avatar:"avatar_3",prize:628},{id:"something7",score:143,rank:7,avatar:"avatar_3",prize:245},{id:"something8",score:143,rank:8,avatar:"avatar_4",prize:245},{id:"something9",score:140,rank:9,avatar:"avatar_3",prize:210}];

const out = {};

// Iterate over the data
for (const obj of data) {

  // Destructure the score an prize from each object
  const { score, prize } = obj;

  // Initialise the key/value of an `out` property
  // with an object with default values
  out[score] ??= { players: 0, prizes: 0 };

  // Increase the players count
  ++out[score].players;

  // Add the prize of the current object to
  // the `prizes` value
  out[score].prizes += obj.prize;

  // Calculate the current score
  out[score].total = out[score].prizes / out[score].players;
}

// Return a new array of objects updating each
// score with the `total` value from the hash object
const result = data.map(obj => {
  return { ...obj, prize: out[obj.score].total };
});

console.log(result);

about 4 years ago · Santiago Gelvez Relatório

0

This should do it

const computeAveragePrizes = (arr) => {
  const grouped = arr.reduce((acc , cur) => {
        const existing = acc[cur.score];
        if (existing) {
          return {
            ...acc ,
            [cur.score]: {
              total: existing.total + cur.prize ,
              count: existing.count + 1
            }
          }
        }

        return {
          ...acc ,
          [cur.score]: {
            total: cur.prize ,
            count: 1
          }
        }
      } , {}
  );

  return arr.map(item => ({
    ...item,
    prize: grouped[item.score].total / grouped[item.score].count
  }))
}
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda