Home > Blockchain >  Compare two arrays of player scores to see who has gone up/down in the list
Compare two arrays of player scores to see who has gone up/down in the list

Time:05-27

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')
     }
   }

  }

  

}

CodePudding user response:

  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:

CodePudding user response:

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#

CodePudding user response:

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
})
  • Related