var leaderboard = [{userId: 10293, balance: 1023},
{userId: 20394, balance: 1806},
{userId: 45333, balance: 2064},
{userId: 57456, balance: 2453},
{userId: 24575, balance: 2703}
];
I have this object and want to create a function that searches to see if a user is already added by checking all the userId
s. If they do exist I want to update their specific balance to a new one, in my case it will be msg.userBalance
. If they don't exist I want to check if their balance is greater than any one of the balances currently in the object, and if so I want to add them to it and remove the 6th one with the lowest balance.
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});
}
}
});
I'm a bit stuck and I know I'm in the right direction but obviously missing a big part of the equation. I'm not opposed to using jQuery I'm just looking for the most simple solution. Help?