for(var i=0, len=localStorage.length; i<len; i++) {
var key = localStorage.key(i);
var value = JSON.parse(localStorage.getItem(key));
console.log(key + " => " + value);
let users = value.username;
let scores = value.score;
let names = value.name;
Image of the local storage:
The Code Above is how i access the local storage values e.g. value.score (Variable scores) will give the users score. on another webpage i have a leader board table and I want to rank the scores e.g. the user Meanace to be rank 1 and James to be rank 2 as he has a lower score. i Would also like to do that for all new users as well, and store the top 3 scores for other uses on the page thanks How would i Do that?
Alright guys i managed to figure it out so what I did was store everything in a dictionary using key-value pairs then sort out the ranking by using a loop and pushing the value rank to each dictionary , then to call it I used a for loop to loop through the main array players
players=[];
for(var i=0, len=localStorage.length; i<len; i++) {
var key = localStorage.key(i);
var value = JSON.parse(localStorage.getItem(key));
console.log(key + " => " + value);
players.push({
username: value.username,
score: value.score,
name: value.name
});
players.sort(function(a, b){
return b.score - a.score;
});
var rank = 1;
for (var i = 0; i < players.length; i++) {
// increase rank only if current score less than previous
if (i > 0 && players[i].score < players[i - 1].score) {
rank++;
}
players[i].rank = rank;
}
}
for(var i=0; i<players.length; i++) {
let users = players[i].username;
let scores = players[i].score;
let names = players[i].name;
let ranks = players[i].rank;