When you first create the variables teamname
and totalpoints
, pull them in from localstorage then.
let [ teamname = 'defaultName', totalpoints = 0 ] = JSON.parse( localStorage.getItem('teamKey') || '[]');
Then when you store the values:
function store()
{
localStorage.setItem( "teamKey", JSON.stringify( [ teamname, totalpoints ] ));
document.write(localStorage.getItem("key value"));
}
You'll want to stringify/parse when entering and grabbing values from localStorage because it becomes a string and this way you can get back the object you are putting.
Edit: I feel like we all misinterpreted what you were asking for:
In order to append to the array every time you store, you need the leaderBoard to be an array of objects where the objects are {teamName:string,totalPoints:number}. And then add to the array every time you store the row. You can also do some checking if you want to see if there's the team already in the leader board and base whether you add them again on that.
let leaderBoard = JSON.parse( localStorage.getItem('leaderBoard') || '[]');
Then when you store the values:
function store(teamName,totalPoints)
{
localStorage.setItem( "leaderBoard", JSON.stringify( [...leaderBoard,{teamName,totalPoints} ));
// depending on what you want to afterwards, you probably need to update leaderBoard variable to the latest as well.
leaderBoard = JSON.parse(localStorage.getItem("leaderBoard"));
document.write(localStorage.getItem("leaderBoard"));
}
You'll want to stringify/parse when entering and grabbing values from localStorage because it becomes a string and this way you can get back the object you are putting.