Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
How to programmatically set a localStorage with increasing values?

I want a localStorage to be programmatically defined. I have an array of values in it Just want the localStorage to take it and define new storage. Like if a click a button it should define a localStorage as 'session 0' with the value from the array and If I click it the second time it should define another localStorage as 'session 1'. But after I close the window and open it and click the button the new localStorage should be continued like 'session 2' not start over from 0.

And it should check if the previous localStorage does have a value or not. Like if I am defining 'session 3' it should check if 'session 2' exists and have a value.

But what problem I am overcoming is after I close the window and start the process the localStorage doesn't start off from where it left if starts from 0 and redefines the storage that was declared first.

Fakearr consists of the value.

This is what I have tried.

for (let i = 0; i <= fakearr.length; i++) {
                        if (
                            (localStorage.getItem(`session ${i}`) == null &&
                                localStorage.getItem(`session ${i - 1}`).length >= 0) ||
                            localStorage.length == 0
                        ) {
                            localStorage.setItem(`session ${i}`, JSON.stringify(fakearr[i]));
                        }
                    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda