我有两个数组,它们是在不同的时间段内获取的.如何判断列表中哪些球员上升/下降,并将新球员标记为上升?
P、 数组已经根据分数进行了排序.
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'
},
]
我try 如下,但似乎无法找到新玩家用例的解决方案.
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')
}
}
}
}