var leaderboard = [{userId: 10293, balance: 1023},
{userId: 20394, balance: 1806},
{userId: 45333, balance: 2064},
{userId: 57456, balance: 2453},
{userId: 24575, balance: 2703}
];
我有这个对象,并且想创建一个函数,通过检查所有的userId
来搜索是否已经添加了一个用户。如果他们确实存在,我想更新他们的具体余额到一个新的,在我的情况下,它将是msg.userBalance
。如果它们不存在,我要检查它们的余额是否大于对象中当前的任何一个余额,如果是,我要将它们添加到对象中,并删除余额最低的第6个。
leaderboard.map(function(person) {
if (person.userId == msg.userId) {
person.balance = msg.userBalance
} else {
if (currentBalance > //other 5 balances in object {
leaderboard.push({userId: msg.userId, balance: msg.userBalance});
}
}
});
我有点卡住了,我知道我在正确的方向,但显然错过了方程式的很大一部分。我并不反对使用jQuery,我只是在寻找最简单的解决方案。帮助吗?
我想这会达到你的目的,
var user = leaderboard.filter(function(user){return userId == msg.userId})[0];
if(user){
user.balance = msg.userBalance;
} else {
leaderboard.push({userId: msg.userId, balance: msg.userBalance});
leaderboard.sort(function(a,b){return a.balance-b.balance});
leaderboard.shift();
}
我在这里做的是,我使用过滤器来获得与目标用户id匹配的用户,,如果没有找到,我将它添加到数组中,然后对数组进行升序排序,然后删除第一个元素,,这将是最低的
这可能不是最有效的方法,但是已经很晚了,而且我很累。
var lowest = Number.POSITIVE_INFINITY; // set the lowest value to something really high
$.each(leaderboard, function(idx,item){ // get the lowest value
if(item.balance < lowest){
lowest = item.balance;
}
});
var i = $.grep(leaderboard, function(item){ // get the item based on userId, if it already exists
return item.userId == msg.userId;
})[0];
if(!i){ // if it doesnt exist...
if(msg.userBalance > lowest){ // if the new items balance is greater than the lowest one
leaderboard.push(msg); // add it
leaderboard = $.grep(leaderboard, function(item){ // and remove the item with the lowest value from the array
return item.balance != lowest;
});
}
} else { // if it does exists
i.balance = msg.userBalance; // update the balance
}
这是一把能用的小提琴。
让它按照你开始的方式工作是可能的——我从来都不是$的粉丝。地图,没有特别的原因。此解决方案只是可以实现的众多方法之一,并且可能帮助您了解所需的逻辑。
解决方案一次遍历数组。但是我们需要从头遍历一次来初始化子对象的值。
leaderboard.max = -Infinity; // Set max balance
leaderboard.min = Infinity; // Set min balance
leaderboard.forEach( function( item, index, self ) {
if (item.balance>self.max) self.max = item.balance;
if (item.balance<self.min) self.min = item.balance;
});
leaderboard.updateBalance = function ( userId, newBalance ) {
var updated = false;
var minIndex1 = -Infinity;
var minIndex2 = Infinity;
this.forEach( function( item, index, self ) {
// Find 2 index with minimal balance
if (item.balance === self.min) minIndex1 = index;
if (item.balance > self.min && item.balance < minIndex2) minIndex2 = index;
// If find user - update balance and min and max
if (item.userId === userId) {
updated = true; // Flag - user balance updated
self[index].balance = newBalance;
if (newBalance > self.max) self.max = newBalance;
if (newBalance < self.min) self.min = newBalance;
}
});
// No user find? Push
if (!updated && newBalance > leaderboard.max) {
this.min = this[minIndex2].balance;
if (this.length===5) this.splice(minIndex1, 1); // Remove user with minimal balance
this.max = newBalance;
this.push( { userId: userId, balance: newBalance } );
}
};
https://jsfiddle.net/5cfyLodp/您可能会考虑将对象列表重组为散列表。它具有一个好的对象的所有条件,因为您的对象只是键值对。
那么你就可以更有力、更简单地根据键来参考你的平衡,以实现你所追求的。
// Initialise hash table
var leaderboard = {
{10293, 1023}
{20394, 1806},
{45333, 2064},
{57456, 2453},
{24575, 2703}
};
// Initialise person and msg somewhere
// If the person exists already, update their balance
if(leaderboard[person.userId] != null) {
leaderboard[person.userId] = msg.userBalance;
return; // Return early, we've done our work
}
// If the person's balance is greater than any of the others in the hash, replace them
for(var i in leaderboard) {
if(leaderboard[i] < msg.userBalance) {
delete leaderboard[i];
leaderboard[person.userId] = msg.userBalance
}
}