比较两个玩家分数数组,看看谁在列表中 up/down

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

我有两个数组,它们是在不同的时间段拍摄的。如何检查列表中哪些球员已经离开 up/down 并包括将新球员标记为上升?

P.S。 - 数组已经根据分数排序。

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
    },
  ]

我需要检查比较两个数组的索引变化并获得如下输出。

   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'
    },
  ]

我正在尝试如下,但似乎无法为新玩家用例找到解决方案。

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

  }

  

}
  1. pastData 数组中创建一个 Map,将 playerName 映射到它们的 index

  2. 迭代 presentData 数组并使用地图计算每个玩家的位置。

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);

其他相关文件:

您可以创建一个 pastDataScoreHashArray.prototype.reduce() and create a result array with Array.prototype.map() 添加 position 属性 给每个玩家并通过比较 pastDataScoreHash 的过去 score 当前 score

代码:

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)

这是一个比第一个快 4 倍的解决方案。

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