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

91
Visualizações
delete individual items of the array in localStorage

I need to delete individual items of the whole array in localStorage. Is it possible to achieve that. I tried the below code. But its deleting the whole watchlist. I have attached a image of the local storage elements

function removeFromWatchlist(par) {
  localStorage.removeItem("watchList")[par];
}

SEE THIS IMAGE

almost 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Sure you can, but you need to update the actual stored value - in your example you are removing the entire stored array.

However it would depend on exactly what par is - as you have an array of objects you will have to decide what equal means.

For example.

function removeFromWatchlist(par) {
  const list = JSON.parse(localStorage.getItem('watchList'));
  const test = JSON.stringify(par);
  const result = list.filter(x => JSON.stringify(x) === test);
  localStorage.setItem('watchList', JSON.stringify(result));
}

This would remove all occurrences of whatever par is from the array stored as watchList in local storage.

almost 4 years ago · Santiago Trujillo Relatório

0

To understand why this is not working, you need to understand how JS will "read" this piece of code as it's being parsed from left to right:

localStorage.removeItem("watchList")[par]
  1. read the variable called localStorage
  2. read the key removeItem from the above step
  3. call the function from the above step with the argument "watchList"
  4. access the key [par] from the return of the above step

The issue here is that on step 3, the entire value is removed from local storage and the function removeItem returns undefined (https://developer.mozilla.org/en-US/docs/Web/API/Storage/removeItem#return_value).

To make it work, you should find an order of operations that fits your task: you want to modify a value in local storage, so you should:

  1. get that value from localStorage
  2. modify the value from above step
  3. store the modified value in localStorage

Which in code would look something like

const value = localStorage.getItem("watchList") // step 1
const modified = /* write your logic here */ // step 2
localStorage.setItem("watchList", modified) // step 3

Also, don't forget that what is stored into and obtained from localStorage is always a string. So if you're storing objects, you should JSON.parse(value) when reading them with getItem and JSON.stringify(value) when storing them with setItem.

almost 4 years ago · Santiago Trujillo Relatório

0

The removeItem() method would remove the whole items of your watchList in local storage, but instead you can find the index of par parameter you are passing and update your watchList array and update it using setItem().

const watchList = JSON.parse(localStorage.getItem('watchList')) || [];
const index = watchList.findIndex(item => item.imdbId === par.imbdId); // in your case imbdId is unique value

if(index !== -1) {
    watchList.splice(index, 1); // removes the item from watchList based on the index
    localStorage.setItem('watchList', JSON.stringify(watchList)); // updateing new watchList
}
almost 4 years ago · Santiago Trujillo 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