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

140
Visualizações
Compare two arrays of player scores to see who has gone up/down in the list

I have two arrays which are taken in different time periods. How to check what player has gone up/down in the list with the inclusion of marking a new player also as going up?

P.S. - Arrays are already sorted according to the score.

pastData:[
  
    {
    playerName:'John Doe',
    score:50
    },
    {
    playerName:'Jane Doe',
    score:40
    },
    {
    playerName:'John Appleseed',
    score:30
    },
    {
    playerName:'John Walker',
    score:20
    },
  ]

presentData:[

    {
    playerName:'Jane Doe',
    score:80
    },
    {
    playerName:'John Doe',
    score:60
    },
    {
    playerName:'John Appleseed',
    score:40
    },
    {
    playerName:'John Mayer',
    score:30
    },
  ]

I need to check the index changes comparing the two arrays and get an output like following.

   presentData:[

    {
    playerName:'Jane Doe',
    score:80,
    position:'up'
    },
    {
    playerName:'John Doe',
    score:60,
    position:'down'
    },
    {
    playerName:'John Appleseed',
    score:40,
    position:'same'
    },
    {
    playerName:'John Mayer',
    score:30,
    position:'up'
    },
  ]

I'm trying as below but can't seem to find a solution for the new player usecase.

let status=[] //this array will include the changes 
which I can use later to update the presentData array 
with a "position:'up/down/same'" key-value pairs.

for (let i = 0; i < 4; i++) { 

  for(let j=0; j<4; j++) {

   if(Object.values(pastData)[i].id==Object.values(presentData)[j].id){
     if(i==j){
      status.push('same')

     }
     if(i<j){
      status.push('down')

      }
     if(i>j){
       status.push('up')
     }
   }

  }

  

}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

  1. Create a Map out of the pastData array which maps the playerName to their index.

  2. Iterate over the presentData array and using the map calculate the position for every player.

const pastData = [
  { playerName: "John Doe", score: 50 },
  { playerName: "Jane Doe", score: 40 },
  { playerName: "John Appleseed", score: 30 },
  { playerName: "John Walker", score: 20 },
];

const presentData = [
  { playerName: "Jane Doe", score: 80 },
  { playerName: "John Doe", score: 60 },
  { playerName: "John Appleseed", score: 40 },
  { playerName: "John Mayer", score: 30 },
];

const pastDataMap = new Map(pastData.map((d, i) => [d.playerName, i]));

const status = presentData.map((d, i) => {
  const pastIndex = pastDataMap.get(d.playerName);
  return {
    ...d,
    position:
      pastIndex === undefined || i < pastIndex
        ? "up"
        : i > pastIndex
        ? "down"
        : "same",
  };
});

console.log(status);

Other relevant documentations:

  • Spread syntax (...)
  • Array.prototype.map
about 4 years ago · Juan Pablo Isaza Relatório

0

You can create a pastDataScoreHash with Array.prototype.reduce() and create a result array with Array.prototype.map() adding position property to each players and getting it's value by comparing the pastDataScoreHash's past score with current score

Code:

const pastData = [
  { playerName: 'John Doe', score: 50 },
  { playerName: 'Jane Doe', score: 40 },
  { playerName: 'John Appleseed', score: 30 },
  { playerName: 'John Walker', score: 20 },
]

const presentData = [
  { playerName: 'Jane Doe', score: 80 },
  { playerName: 'John Doe', score: 60 },
  { playerName: 'John Appleseed', score: 40 },
  { playerName: 'John Mayer', score: 30 },
]

const pastDataHash = pastData.reduce((a, { playerName: p }, i) => ((a[p] = i), a), {})

const getPosition = (past, current) => {
  switch (true) {
    case past == current:
      return 'same'
    case past == undefined || past > current:
      return 'up'
    default:
      return 'down'
  }
}

const result = presentData.map((p, i) => ({
  ...p,
  position: getPosition(pastDataHash[p.playerName], i),
}))

console.log(result)
https://stackoverflow.com/questions/72401269/compare-two-arrays-of-player-scores-to-see-who-has-gone-up-down-in-the-list#

about 4 years ago · Juan Pablo Isaza Relatório

0

This is a solution about x4 faster than first one.

presentData.map((present,index)=> {

    const pastIndexPostion = pastData.findIndex(t => t.playerName === present.playerName)
    if(pastIndexPostion > index || pastIndexPostion===-1){
        present.position = 'up'
    }else if(pastIndexPostion === index){
        present.position = 'same'
    }else {
        present.position = 'down'
    }
    return present
})
about 4 years ago · Juan Pablo Isaza 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