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

109
Visualizações
Append response data in LocalStorage objects

I have a frontend that sends an id automatically when someone visits a URL, like /products/1, /products/2, it send the id like 1 or 2 to the server, then the server send a response for that id back to the browser, now I want to save this response in localStorage, but when someone visits, /1 its saves response in local storage, but now when someone visits, /2 it replaces the previous local storage value instead of adding new, How can I keep on adding new data to localStorage instead of updating the previous value?


fetch("/apps/proxy/sendid", {
        method: "POST",
        headers: {'Content-Type': 'application/json'}, 
        body: JSON.stringify(jspid)
      }).then(function (response) {
        // The API call was successful!
        console.log(response);
        return response.json();
      }).then(function (data) {
        // This is the JSON from our response
         // This is the JSON from our response
        console.log(data);

        const productsDetails = [];

        productsDetails.push(data);
        
        localStorage.setItem('future', JSON.stringify(productsDetails));

      }).catch(function (err) {
        // There was an error
        console.warn('Something went wrong.', err);
      });
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First, get the current items from localStorage and assigned them to productsDetails. Then append the next items to the array.

let productsDetails = localStorage.getItem('future');

if(null === productsDetails) {
    productsDetails = [];
}

productsDetails.push(data);
            
localStorage.setItem('future', JSON.stringify(productsDetails));
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