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

224
Visualizações
How to modify/delete an object from localStorage?

I am currently in apprenticeship and I am having a problem. I must be able to modify the quantity of the product or remove it from my basket. (therefore modify the localStorage) But I cannot do it, I am aware that this question must have been asked several times. But I can't find an answer to my problem among the many documentations, I must go about it wrong.

let basket = JSON.parse(localStorage.getItem("Sofas")) || [];
const cartItems = document.getElementById("cart__items");

contentBaskets = [];

function onQuantityChange(id) {
  
}

for (let i = 0; i < basket.length; i++) {
  fetch("http://localhost:3000/api/products/" + basket[i].id)
    .then((response) => response.json())
    .then((data) => {
      contentBaskets =
        contentBaskets +
        `<article class="cart__item" data-id=${basket[i].id}>
      <div class="cart__item__img">
      <img src=${data.imageUrl}>
      </div>
      <div class="cart__item__content">
        <div class="cart__item__content__titlePrice">
          <h2>${data.name}</h2>
          <p>${data.price}€</p>
        </div>
        <div class="cart__item__content__settings">
          <p>Couleur : ${basket[i].color}</p>
          <div class="cart__item__content__settings__quantity">
            <p>Qté : </p>
            <input type="number" class="itemQuantity" name="itemQuantity" min="1" max="100" value=${basket[i].quantity} onchange=onQuantityChange('${basket[i].id}')>
          </div>
          <div class="cart__item__content__settings__delete">
            <p class="deleteItem">Supprimer</p>
          </div>
        </div>
      </div>
      </article>`;
      if (basket.length) {
        cartItems.innerHTML = contentBaskets;
      }
    });
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'm not sure what's your question, but here's how to edit an object in localStorage.

// Get the string that's stored in localStorage
const stored = localStorage.getItem("sofas")
// Parse
const array = JSON.parse(stored) || []
// Push a new sofa to the array
array.push("IKEA Friheten")
// Set to localStorage the stringified version 
// of the array, so you can get it later
localStorage.setItem("sofas", JSON.stringify(array))
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