How to programmatically set a localStorage with increasing values?
What about something like this:
function maxSessionID(){
let i = 0;
while (localStorage.getItem(`session${i}`) !== null ) {
i++;
}
return i;
}
for (let i = 0; i < fakearr.length; i++) {
localStorage.setItem(`session${maxSessionID()+1}`, JSON.stringify.fakearr[i]);
}
There is check for highest ID number sessionID
before adding each item from fakearr
so this doesn't replace existing sessions when there is hole in IDs (in case of session0
, session2
, session3
. It adds session1
and then continues from session4
)