I am trying to push player data (object) in an array, then save into localStorage
, but I can't get it working even if I initialize the array. On the first run I got the data, but on the next runs the array is not recognised. Does anyone know what is going on? Here is my code:
function handleRanking() {
console.log('run function');
let prevRanking = localStorage.getItem('ranking');
if (prevRanking === null) {
console.log('null value');
prevRanking = [];
}
const player = {
name,
score: total,
picture: gravatarEmail,
};
if (Array.isArray(prevRanking)) {
prevRanking.push(JSON.stringify(player));
localStorage.setItem('ranking', prevRanking);
} else {
console.log('not an array');
}
}