Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!